注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

love3400wind的blog

like

 
 
 

日志

 
 
 
 

Maven Archetypes Part 3: How do I set up a multi-module project? (2)  

2016-01-04 17:35:13|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

creating the webapp module

We've now completed adding our first module to the build. To follow the case above of having a model and webapp module, let's create the initial webapp directories:

mkdir -p src/main/resources/archetype-resources/__artifactId__-webapp/src/main/java/__packageInPathFormat__

If we're making a webapp, then we'll need the servlet api. Let's define this in the parent pom's dependency management section, with the provided scope. We'll also define the version of the model module so that we can import it into the webapp project without specifying the version:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-model</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
</dependencyManagement>

We also need to add the webapp module to our list of modules in the parent:

<modules>
<module>${artifactId}-model</module>
<module>${artifactId}-webapp</module>
</modules>

Let's now create the pom for our webapp at src/main/resources/archetype-resources/__artifactId__-webapp/pom.xml. We'll add a dependency on the servlet-api, our model module, and add the Maven Jetty plugin so we can start it up from Maven after we create the project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}-webapp</artifactId>
<packaging>war</packaging>
<name>${artifactId}-webapp</name>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-model</artifactId>
</dependency>
</dependencies>
</project>
view rawpom-webapp.xml hosted with ? by GitHub

If we're dealing with a webapp, we need a web.xml file located in src/main/webapp/WEB-INF in our project for the Maven war plugin. Let's create the directory we'll need for this and add the web.xml file:

mkdir -p src/main/resources/archetype-resources/__artifactId__-webapp/src/main/webapp/WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<display-name>${artifactId}</display-name>
<servlet-name>${artifactId}</servlet-name>
<servlet-class>${package}.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>${artifactId}</servlet-name>
<url-pattern>/${artifactId}</url-pattern>
</servlet-mapping>
</web-app>
view rawweb.xml hosted with ? by GitHub

We can filter this file as well to name the servlet after our project, hence the ${artifactId} tokens in the web.xml file.

We also need a very basic servlet class that matches what we defined in the web.xml file. Go ahead and create this class at src/main/resources/archetype-resources/__artifactId__-webapp/src/main/java/__packageInPathFormat__/Servlet.java with the contents below:

package ${package};
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setStatus(200);
PrintWriter writer = response.getWriter();
writer.write(App.message());
}
}
view rawServlet.java hosted with ? by GitHub

updating the archetype's metadata (yes, again), and reviewing the file structure

We're almost in the home stretch here, I promise!

We need to update src/main/resources/META-INF/maven/archetype-metadata.xml one more time to add the resources for our webapp module. Here's what the final archetype-metadata.xml file will look like:

<archetype-descriptor name="sample-archetype">
<fileSets>
<fileSet filtered="true">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp/src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp/src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>

Let's review the topology of our archetype one last time, just to make sure everything is in place:

$ tree
.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____archetype-resources
| | | | |______artifactId__-model
| | | | | |____pom.xml
| | | | | |____src
| | | | | | |____main
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____App.java
| | | | | | |____test
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____AppTest.java
| | | | |______artifactId__-webapp
| | | | | |____pom.xml
| | | | | |____src
| | | | | | |____main
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____Servlet.java
| | | | | | | |____webapp
| | | | | | | | |____WEB-INF
| | | | | | | | | |____web.xml
| | | | |____pom.xml
| | | |____META-INF
| | | | |____maven
| | | | | |____archetype-metadata.xml

installing the archetype, creating a project, and running the webapp

Now let's install our archetype, go back a directory, and create a project from it.

$ mvn clean install
$ cd ..
$ mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> com.theotherian.archetype:sample-archetype (sample-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1: 1
Define value for property 'groupId': : com.theotherian
Define value for property 'artifactId': : sample-project
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.theotherian: :
Confirm properties configuration:
groupId: com.theotherian
artifactId: sample-project
version: 1.0-SNAPSHOT
package: com.theotherian
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: sample-archetype:1.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.theotherian
[INFO] Parameter: artifactId, Value: sample-project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.theotherian
[INFO] Parameter: packageInPathFormat, Value: com/theotherian
[INFO] Parameter: package, Value: com.theotherian
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.theotherian
[INFO] Parameter: artifactId, Value: sample-project
[WARNING] Don't override file /Users/isimpson/sample-projects/sample-project/pom.xml
[INFO] project created from Archetype in dir: /Users/isimpson/sample-projects/sample-project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.574s
[INFO] Finished at: Sat May 12 22:14:07 MDT 2012
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------

Awesome! We have our project, so let's compile it!

$ cd sample-project
$ mvn clean install

Now that the artifacts are installed, let's run the webapp!

$ cd sample-project-webapp
$ mvn jetty:run-war

Now, if everything was set up correctly, you should be able to see "Hello world!" in your browser by going to the following URL after the Jetty server has launched: http://localhost:8080/sample-project-webapp/sample-project

Congratulations on getting through these articles and writing your first archetype! Hopefully this will put you on the path to making archetypes that will reduce the overhead of creating projects based on certain standards and settings.

  评论这张
 
阅读(88)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017