1.使用IntelliJ IDEA来创建maven的Java web应用
1.1.示例创建与配置
首先File -> New Project,在弹出框中左面选择Maven;然后在面板上勾选(Create from archetype)从原型创建,接下来在下面的选项中选择org.apache.maven.archetypes:maven-archtype-webapp(见下图);点击下一步写好名称等参数直至创建结束。
接下来配置pom.xml文件来添加jetty插件使得可以使项目能够运行:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<stopKey>STOP</stopKey>
<stopPort>8005</stopPort>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>
1.2.示例运行
这时候可以使用jetty插件来运行示例。
简单的在命令行输入mvn jetty:run或者在IntelliJ IDEA右边的maven面板中选择项目下的插件双击jetty:run(下图位置)也可以运行起来:
输出类似于下面这样:
[INFO] --- maven-jetty-plugin:6.1.10:run (default-cli) @ example ---
[INFO] Configuring Jetty for project: example Maven Webapp
[INFO] Webapp source directory = D:\work\myproject\edis\example\src\main\webapp
[INFO] web.xml file = D:\work\myproject\edis\example\src\main\webapp\WEB-INF\web.xml
[INFO] Classes directory D:\work\myproject\edis\example\target\classes does not exist
[INFO] Logging to org.slf4j.impl.MavenSimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog
[INFO] Context path = /example
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] Webapp directory = D:\work\myproject\edis\example\src\main\webapp
[INFO] Starting jetty 6.1.10 ...
[INFO] jetty-6.1.10
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
在浏览器地址栏输入http://localhost:8080/example/,页面出现hello world字样
2.多模块的Java web应用
我在练习的时候习惯在一个项目中创建多个模块,创建多个模块很容易。右键单击项目,在弹出菜单中选择New -> Module...之后的步骤有与前面的创建项目一样。运行单个模块可以双击右面maven面板上对应模块的插件中jetty:run