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

love3400wind的blog

like

 
 
 

日志

 
 
 
 

Maven Archetypes Part 2: How do I create my first jar?  

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

  下载LOFTER 我的照片书  |

introduction and review

Before you start with this article, you need to have a basic understanding of Maven archetypes or have read the first article in my guide.  If you haven't, I have a feeling most of this won't make sense to you.

That said, let's start off with a quick review.  In the last article, we left things with an archetype layout of:

$ tree
.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____archetype-resources
| | | | |____pom.xml
| | | |____META-INF
| | | | |____maven
| | | | | |____archetype-metadata.xml

This is the most basic archetype you can really have; all it does is create a pom artifact. Typically, you won't be interested in generating a pom from an archetype; you'll want to at least create a jar, and potentially a multi-module project. For the next step, let's create our first jar.

Updating the project pom file to create a jar

Before we start to add files for our archetype, let's update src/main/resources/archetype-resources/pom.xml to change the packaging to jar. Let's also add a dependency on junit so that we can package a unit test in the project that's created, as well as the compiler setting:

<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>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>${artifactId}</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Creating the necessary directories and updating the archetype metadata

Next, let's add the default directories that Maven projects use. I've added __packageInPathFormat__ as a subdirectory of both the main and test paths. When you generate a project from an archetype, Maven will create the property packageInPathFormat for you, which changes all of the dots in a package name to slashes. For example, if you have a package of com.theotherian, the packageInPathFormat property will be com/theotherian.

$ mkdir -p src/main/resources/archetype-resources/src/main/java/__packageInPathFormat__
$ mkdir -p src/main/resources/archetype-resources/src/test/java/__packageInPathFormat__

We also need to update the src/main/resources/META-INF/maven/archetype-metadata.xml file so that it knows to include and filter our new directories:

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

creating a basic Java class and a unit test

Now that we have our directories set up, let's create some source code.
First, let's add your standard "Hello world!" Java class in the file src/main/resources/archetype-resources/src/main/java/__packageInPathFormat__/App.java:

package ${package};
public class App {
public static void main(String args[]) {
System.out.println(message());
}
public static String message() {
return "Hello world!";
}
}
view rawApp.java hosted with ? by GitHub

Next, let's add a basic unit test just to flesh things out a bit more at src/main/resources/archetype-resources/src/test/java/__packageInPathFormat__/AppTest.java:

package ${package};
import static org.junit.Assert.*;
import org.junit.Test;
public class AppTest {
@Test
public void message() throws Exception {
assertEquals("Hello world!", App.message());
}
}
view rawAppTest.java hosted with ? by GitHub

Wrapping things up

Now that we've added all of this additional stuff, let's do a quick recap of the project layout:

$ tree
.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____archetype-resources
| | | | |____pom.xml
| | | | |____src
| | | | | |____main
| | | | | | |____java
| | | | | | | |______packageInPathFormat__
| | | | | | | | |____App.java
| | | | | |____test
| | | | | | |____java
| | | | | | | |______packageInPathFormat__
| | | | | | | | |____AppTest.java
| | | |____META-INF
| | | | |____maven
| | | | | |____archetype-metadata.xml

If your project looks like that, go ahead and install the archetype and go back a directory.

$ mvn clean install
$ cd ..

creating a project from the archetype

Let's generate another project from our local archetype (if you haven't included this archetype in your local catalog, follow the steps in part 1):

$ 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-jar
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.theotherian: :
Confirm properties configuration:
groupId: com.theotherian
artifactId: sample-jar
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-jar
[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-jar
[WARNING] Don't override file /Users/isimpson/sample-projects/sample-jar/pom.xml
[INFO] project created from Archetype in dir: /Users/isimpson/samples-projects/sample-jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:29.976s
[INFO] Finished at: Sat May 12 12:00:34 MDT 2012
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------

Our project from the archetype has been created, so let's try building it:

$ cd sample-jar
$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-jar 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ sample-jar ---
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ sample-jar ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/isimpson/sample-projects/sample-jar/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ sample-jar ---
[INFO] Compiling 1 source file to /Users/isimpson/sample-projects/sample-jar/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ sample-jar ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/isimpson/sample-projects/sample-jar/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ sample-jar ---
[INFO] Compiling 1 source file to /Users/isimpson/sample-projects/sample-jar/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ sample-jar ---
[INFO] Surefire report directory: /Users/isimpson/sample-projects/sample-jar/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.theotherian.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ sample-jar ---
[INFO] Building jar: /Users/isimpson/sample-projects/sample-jar/target/sample-jar-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.740s
[INFO] Finished at: Sat May 12 12:05:52 MDT 2012
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------

Awesome, you've got an archetype building a jar!

But wait, there's more! Check out the next post in this series, which shows you how to build multi-module archetypes, to learn more!

Posted by at  
  评论这张
 
阅读(127)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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