文档章节

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

石穿
 石穿
发布于 2017/07/25 15:56
字数 1333
阅读 19
收藏 0

原文地址

###介绍与重温 在你开始这个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
博文 10
码字总数 6925
作品 0
高级程序员
eclipse 创建maven 工程 时 报错,无法创建。

小编在用eclipse创建maven工程时,提示无法创建,缺少jar包: 一、在创建maven选择Select an Archetype时,选择: 无法创建提示:Could not resolve archetype org.apache.maven.archetypes...

LQH_我看行
2016/11/06
138
0
eclipse 配置Maven问题解决办法:新建maven工程时报错:Could not resolve archetype org.apache.maven.archetypes .

此文乃本作者配置maven,被其折磨n天,究极解决方案,好文要顶啊。欢迎致电: zhe-jiang.he@hp.com 首先各maven.archetypes下载地址: http://mirrors.ibiblio.org/maven2/org/apache/maven...

庞陆阳
2016/09/30
126
0
解决Maven的Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart

eclipse配置好了Maven,创建maven-archetype-quickstart项目报错如下: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE from any of the conf......

技术小哥哥
2017/11/14
0
0
Maven常见错误

Eclipse使用Maven创建Web时错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap 解决方案: 1. 可能是网路不通 2. 在网络不通时创建Maven项目报错,然后网...

残风vs逝梦
2016/06/28
78
0
Eclipse+Maven+Tomcat集成

1 Eclipse下集成Maven 1.1 安装Maven 下载maven的bin,在apache官方网站可以下载。 下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVENHOME 在PATH里加入m...

山山山
2016/03/04
242
0

没有更多内容

加载失败,请刷新页面

加载更多

Ubuntu18.04下载安装Google图解法

首先Ctrl + Alt + T打开终端,或者可以在在搜索框搜索终端。 (1)sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/ ,(将下载源加入到系统的...

AI_SKI
7分钟前
0
0
spring could采坑 eureka开启验证后无法连接注册中心

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClie......

君千殇520
8分钟前
0
0
支付宝小程序下单支付接口:40004 ACQ.INVALID_PARAMETER

下面是支付宝下单接口 https://docs.open.alipay.com/api_1/alipay.trade.create/ 如果按官方文档的说明,并没有解释清楚buyer_id什么时候要传,只是说:特殊可选,啥叫特殊可选?!在调小程...

swingcoder
16分钟前
0
0
【Java】广州三本秋招经历

前言 只有光头才能变强 离上次发文章已经快两个月时间了,最近一直忙着秋招的事。今天是2018年10月22日,对于互联网行业来说,秋招就基本结束了。我这边的流程也走完了(不再笔试/面试了),所...

Java3y
18分钟前
0
0
在idea中启动多个SpringBoot项目

https://blog.csdn.net/forezp/article/details/76408139

灯下草虫鸣_
20分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部