Maven Archetypes Part 2:怎样创建我的第一个jar?

原文地址

介绍与重温

在你开始这个article之前,你需要通过阅读Maven Archetypes Part 1:从哪里开始?对Maven archetypes有一个基础的认识。

让我们快速重温上一节的内容。在上一节中,我们建立了一个结构如下的archetype:

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

这是你能拥有的最基础的archetype。他做的工作就是创建了一个带有pom的artifact。通俗的讲,你不会对只创建一个pom的archetype感兴趣,你需要的是至少创建一个jar,甚至一个多模块(multi-module)的项目。下一步,我们将创建我们的第一个jar。

更新项目的pom文件去创建一个jar

在我们向archetype添加文件前,让我们先更新src/main/resources/archetype-resources/pom.xml让生成的项目打包为一个jar(生成后的项目在使用maven编译时)。同时我们也为生成出来的项目添加一个junit依赖,让我们在生成出来的项目里能使用单元测试:

<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>

创建一个必须的文件夹,然后更新archetype metadata

接下来,让我们增加一个Maven项目使用的几个默认的文件夹。我给main和test路径同时添加了__packageInPathFormat__作为子文件夹。当你利用archetype生成一个项目的时候,Maven会为你产生一个packageInPathFormat参数,去替代使用下划线命名的包。举个例子,如果你有一个package名为com.theotherianpackageInPathFormat参数将会为com/theotherian

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

我们也需要更新src/main/resources/META-INF/maven/archetype-metadata.xml文件,他知道生成的时候要包含和过滤哪些文件夹:

<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>

创建一个基本的Java类和一个单元测试类

现在我们已经拥有了文件夹,让我们再码两行代码。
首先,创建一个java类src/main/resources/archetype-resources/src/main/java/__packageInPathFormat__/App.java,在类里面say个Hello world!

package ${package};

public class App {

  public static void main(String args[]) {
    System.out.println(message());
  }

  public static String message() {
    return "Hello world!";
  }
}

然后创建一个测试类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());
  }
}

打包

现在我们添加了所有东西,让我们快速浏览一下项目结构:

$ 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

如果你的项目长得跟这一样,回到archetype根目录,将archetype install到本地仓库中:

$ mvn clean install
$ cd ..

使用archetype生成一个项目

让我们从本地archetype生成另外一个项目(如果你还没有将这个archetype包含到本地catalog中,请阅读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] ------------------------------------------------------------------------

我们的项目已经创建了,让我们build它:

$ 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] ------------------------------------------------------------------------

狂拽酷炫吊炸天,你已经使用archetype构建了一个jar!
下一节我们讲怎样构建一个多模块的archetypes。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值