maven---3手写一个helloWord

1.编写POM

Maven项目的核心文件是pom.xml,POM(Project Objcet Model)项目对象模型,该文件中定义了项目基本信息包括如何构建、项目依赖、打包类型、项目继承等等。

为helloword项目编写一个pom.xml文件

在I:/目录下新建一个helloword文件夹,在文件夹中建一个pom.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
  <name>maven helloword project</name>
</project>

解释

  • modelVersion指定了当前Pom模型的版本,对于maven2、maven3写4.0.0
  • groupId 、artifactId、version三个元素定义了一个项目或者构件(artifact )坐标,在maven中任何jar、pom、war都是以基于这些基本的坐标进行区分的。
  • groupId定义了项目属于哪个组,这个组一般和项目所在公司存在联系。如baidu建立了一个名为weixin的项目,那么groupId=com.baidu.weixin。
  • artifactId定义了当前maven项目在组中的唯一ID,我们为这个hello world项目定义artifactId为helloword.
  • packaging 指定了项目的打包类型,默认是jar,打包类型有jar、war、pom
  • version指定了helloword项目当前版本,SNAPSHOT为快照版,说明项目处于开发期。
    版本号解释
    x.x.x-里程碑--->
    第一个x表示项目版本有大变动如架构变化1.0->2.0,第二个x表示项目分支变化,第三个x表示项目分支的变化次数。
    里程碑:SNAPSHOT开发版本,alpha项目组内部测试版本,beta公开使用测试版本,Release(RC)稳定版,GA可靠版本
  • name项目名称,方便用户可以看的懂。

2.编写主代码

  • 项目主代码和测试代码不同,项目主代码会被打包到最终的构件中(如jar),而测试代码值在运行测试时用到,不会打包。
  • 那么主代码要写在哪里才会被maven识别呢?默认maven假设项目主代码位于src/main/java目录下,所以我们遵循约定在该目录下创建文件com/zlcook/studymvn/helloword/HelloWord.java
    内容如下:
package com.zlcook.studymvn.helloword;

public class HelloWord
{
    public String say(){
        return "hello maven";
    }
    public static void main(String[] args){
        System.out.print(new HelloWord().say());
    }
}

一个简单的java类

java代码注意

  • 在绝大多数情况下,应该把项目主代码放到src/main/java目录下(遵循maven约定),无须额外配置,maven会自动搜寻该目录找到项目主代码
  • java类的包名com.zlcook.studymvn.helloword,这与之前在POM中定义的groupId和artifactId相吻合。建议都这么写,这样更加清晰符合逻辑,也方便搜索构建或java类。
一个简单的maven项目

2.1编译主代码

打开命令行,进入到项目根目录下运行mvn clean compile得到如下结果

I:\helloword>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.383 s
[INFO] Finished at: 2016-11-15T16:30:35+08:00
[INFO] Final Memory: 14M/220M

编译完后文件夹

编译后多出target文件夹

解释mvn clean compile命令

  • clean告诉maven清理输出目录 target/
  • compile告诉maven编译项目主代码。

依次执行的目标任务有

  • maven-clean-plugin:2.5:clean 删除target文件夹,默认maven构建的所有输出都在target/目录中
  • maven-resources-plugin:2.6:resources 项目主资源处理,未定义项目资源,会跳过,skip non existing resourceDirectory;
  • maven-compiler-plugin:3.1:compile 编译主代码,将项目主代码编译到target/classes目录。
  • maven-clean-plugin:2.5:clean 、maven-resources-plugin:2.6:resources、maven-compiler-plugin:3.1:compile对应了一些Maven插件及插件目标,maven-clean-plugin:2.5:clean是maven-clean-plugin插件的clean目标。

3.编写测试代码

  • 为了是项目结构清晰,maven的测试代码默认目录是src/test/java。所以在helloword文件夹下创建该目录。

3.1添加测试依赖

  • 编写的测试代码需要依赖JUnit,所以修改pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>maven helloword project</name>
  <dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency> 
</dependencies>
</project>
  • dependencies元素下可以包含多个dependency元素以声明项目的依赖,每个依赖都是由groudId,artifactId,version组成了构件的唯一性。有了这段配置信息maven就会自动下载junit-4.7.jar。那么maven会去哪里下载呢?答:maven会自动访问中央仓库(http://repol.maven.org/maven2/)下载需要文件。
  • dependency元素中还有scope配置,scope为依赖范围。scope的取值如下:
    1.test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖,换句话说在测试代码中导入import JUnit代码没问题,但是在主代码中使用import JUnit代码会造成编译错误,同时在主代码中导入了测试代码中的类,在编译时也会出错,比如“符号找不到”。因为在编译主代码时测试代码包括scope为test的依赖都不会使用。 如:junit
    2.compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去。(默认方式)
    3.provided依赖,在编译和测试过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等服务器已经存在了,如果再打包会冲突
    4.runtime在运行时依赖,在编译的时候不会依赖,测试运行时才依赖,如mysql-connector-java

注:在项目之间只有complie范围的依赖才会传递

3.2编写测试代码及测试

在src/test/java目录下写测试代码文件
src\test\java\com\zlcook\studymvn\helloword\HelloWordTest.java

package com.zlcook.studymvn.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class HelloWordTest
{
    @Test
    public void testSay(){
        HelloWord helloWord = new HelloWord();
        String result = helloWord.say();
        assertEquals("hello maven",result); 
        
    }
}

使用JUnit4,需要执行测试的方法都需要加上@Test注解。
测试案例写完,执行maven测试命令,在helloword文件下执行命令:mvn clean test
得到结果如下:

I:\helloword>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO] Deleting I:\helloword\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
lloword ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellowo
rd ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloword ---
[INFO] Surefire report directory: I:\helloword\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.zlcook.studymvn.helloword.HelloWordTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.699 s
[INFO] Finished at: 2016-11-16T13:49:29+08:00
[INFO] Final Memory: 15M/191M
[INFO] ------------------------------------------------------------------------

依次执行的目标有

  • maven-clean-plugin:2.5:clean 删除target文件夹
  • maven-resources-plugin:2.6:resources 项目主资源处理
  • maven-compiler-plugin:3.1:compile 编译主代码
  • maven-resources-plugin:2.6:testResources 测试资源处理
  • maven-compiler-plugin:3.1:testCompile 编译测试代码,编译通过在target/test-classes下生成了二进制文件
  • maven-surefire-plugin:2.12.4:test 运行测试代码,surefire是maven中负责执行测试的插件,并生成测试报告surefire-reports,显示共运行了多少测试,失败了多少,出错了多少,跳过了多少。

由此可见,在执行test之前会先执行compile。

4.打包和运行

4.1打包

项目编译、测试之后,然后就可以打包(package),Hello Word的打包指定了打包类型为jar包。
maven的打包命令:mvn clean package

执行结果:

I:\helloword>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO] Deleting I:\helloword\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
lloword ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellowo
rd ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloword ---
[INFO] Surefire report directory: I:\helloword\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.zlcook.studymvn.helloword.HelloWordTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloword ---
[INFO] Building jar: I:\helloword\target\helloword-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.159 s
[INFO] Finished at: 2016-11-16T14:17:36+08:00
[INFO] Final Memory: 16M/187M
[INFO] ------------------------------------------------------------------------

依次执行的目标有

  • maven-clean-plugin:2.5:clean 删除target文件夹
  • maven-resources-plugin:2.6:resources 项目主资源处理
  • maven-compiler-plugin:3.1:compile 编译主代码
  • maven-resources-plugin:2.6:testResources 测试资源处理
  • maven-compiler-plugin:3.1:testCompile 编译测试代码,编译通过在target/test-classes下生成了二进制文件
  • maven-surefire-plugin:2.12.4:test
  • maven-jar-plugin:2.4:jar 打包插件,将项目主代码打包成一个helloword-0.0.1-SNAPSHOT.jar文件到target/目录下。名称是根据artifact-version.jar规则进行命名的, 如果不想使用该名称,可以在pom.xml文件中使用finalName来定义文件名称,位置
<project>
 <build>
       <finalName>项目名称</finalName>
 </build>
</project>

由此可见在执行打包前,执行了编译、测试。

安装到本地仓库

...
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloword ---
[INFO] Installing I:\helloword\target\helloword-0.0.1-SNAPSHOT.jar to D:\Soft\ma
ven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-
0.0.1-SNAPSHOT.jar
[INFO] Installing I:\helloword\pom.xml to D:\Soft\maven\maven_jar\repository\com
\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...

在执行打包之前,依次执行了编译、测试、打包。

  • maven-install-plugin:2.4:install 安装,把jar包安装到本地仓库中,helloword项目在本体仓库文件如图:
HelloWord项目的pom和jar文件

4.2运行

  • helloword项目中的HelloWord类有一个main方法,默认打包生成的jar是不能直接运行的,因为带有main方法的类信息不会添加到manifest中(解压jar文件后,其中的META-INF/MANIFEST.MF文件没有Main-Class一行)。

MANIFEST.MF文件信息如下:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dell
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_65

为了生成可执行的jar文件,需要借助maven-shade-plugin插件,在pom.xml中配置插件如下:
了解更多插件使用

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven helloword project</name>
<dependencies>
 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
  </dependency> 
</dependencies>
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.zlcook.studymvn.helloword.HelloWord</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
  • 我们配置了mainClass为com.zlcook.studymvn.helloword.HelloWord,项目打包时会将该信息放到MANIFEST中。

在执行mvn clean install命令
执行完后在target/目录中可以出现两个文件

Paste_Image.png

original-helloword-0.0.1-SNAPSHOT.jar 是原始的jar。
helloword-0.0.1-SNAPSHOT.jar 是带有Main-Class信息的可运行的jar

解压helloword-0.0.1-SNAPSHOT.jar 在META-INF/MANIFEST.MF中包含信息:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dell
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_65
Main-Class: com.zlcook.studymvn.helloword.HelloWord

执行jar文件

在控制台输入命令如下,可以看到输出了hello maven结果。


执行jar文件

4.2.*生成可运行的jar文件(补充,可以忽略)

  • 一个javase项目最终通过java命令会打包生成一个jar包,一个javase项目通常会引用第三方jar包,而通过java提供的打包命令不会将第三方jar打包到最终的jar包中。所以最终的jar因为缺少依赖而无法运行。那么如何才能让一个普通的javase项目生成一个可执行的jar包呢?

  • 下面一段话是在spring-boot上面看到的,提到了一种常用的方法是shaded jars,以及spring-boot采用的有别于shaded jars的方法,spring-boot的方法请看spring boot开发文档的第Appendix E. The executable jar format章节,上面使用的maven-shade-plugin插件生成的可运行jar就是使用shaded jars方法。


Java does not provide any standard way to load nested jar files (i.e. jar files that are themselves contained within a jar). This can be problematic if you are looking to distribute a self-contained application that you can just run from the command line without unpacking.

To solve this problem, many developers use “shaded” jars. A shaded jar simply packages all classes, from all jars, into a single 'uber jar'. The problem with shaded jars is that it becomes hard to see which libraries you are actually using in your application. It can also be problematic if the same filename is used (but with different content) in multiple jars. Spring Boot takes a different approach and allows you to actually nest jars directly.


  • 为了验证shade jar的原理,给HelloWorld类的内容做了改变,依赖了 org.junit.Test等类,这样就加入了第三方依赖。然后使用maven-shade-plugin打包生成的jar包和没使用生成的jar包内容的对比。
package com.zlcook.studymvn.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.Date;
public class HelloWord
{
    public String say(){
        return "hello maven";
    }
    public static void main(String[] args){
        HelloWord helloWord = new HelloWord();
        String result = helloWord.say();
        System.out.print(result);

        assertEquals("hello maven",result); 
    }
}
没使用maven-shade-plugin插件生成的jar内容
使用maven-shade-plugin插件生成的jar内容

com、junit、org文件里是编译后的.class文件,其中shade打包的META-INF/MANIFEST.MF中添加了Main-Class信息,正如上面一样。

5使用Archetype创建maven项目

5.1需求背景

  • 在写helloword项目中我们遵循了maven的约定,比如主代码写在src/main/java,测试代码写在src/test/java下面,pom.xml文件放置项目更目录下等。这些目录结构和pom.xml文件内容称为项目的骨架,当然java项目和javaEE项目的项目骨架是有不同的地方。当我们用maven创建java项目时就必须遵循这些约定,也就是每次都要创建这些目录和pom.xml文件。

  • 那么问题来了?每次都自己创建不是挺麻烦的吗?maven不是有好多命令吗可不可以执行某一个命令就可以帮我们自动创建创建好目录结构和pom.xml文件。
    答:Maven提供了maven-archetype-plugin插件以帮助我们快速勾勒出项目骨架(当然你也可以自己开发一个)。

5.2案例演示

  • 以study-archetype项目为例使用maven archetype在D:\soft盘创建study-archetype项目骨架。

在命令行输入mvn archetype:generate命令。
结果如下:

D:\Soft>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Interactive mode
.......
1717: remote -> tr.com.obss.sdlc.archetype:obss-archetype-java (This archetype p
rovides a common skelton for the Java packages.)
1718: remote -> tr.com.obss.sdlc.archetype:obss-archetype-webapp (This archetype
 provides a skelton for the Java Web Application packages.)
1719: remote -> uk.ac.ebi.gxa:atlas-archetype (Archetype for generating a custom
 Atlas webapp)
1720: remote -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)
1721: remote -> uk.co.nemstix:basic-javaee7-archetype (A basic Java EE7 Maven ar
chetype)
1722: remote -> uk.co.solong:angular-spring-archetype (So Long archetype for RES
Tful spring services with an AngularJS frontend. Includes debian deployment)
1723: remote -> us.fatehi:schemacrawler-archetype-maven-project (-)
1724: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
1725: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
1726: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 888:
  • 会出现很多可用的archetype供选择。
    正如在eclipse中可视化创建maven项目时会出现选择框让你选择合适的archetype。


    eclipse中创建maven程序选择合适archetype
  • 每个archetype前面都会对应一个编号,同时命令行会提示一个默认的编号888,其对应的archetype是maven-archetype-quickstart,该archetype适合用来创建普通java项目的maven结构,如果是javaEE项目可以选择maven-archetype-webapp。

  • 直接回车选择888,紧接着会让你选择maven-archetype-quickstart插件的版本,该骨架插件的版本有好几个我们选择编号6。

Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
  • 接下来就会提示你输入要创建的项目的groupId、artifactId、version以及包名package
    确定项目的信息
Define value for property 'groupId': : com.zlcook.studymvn
Define value for property 'artifactId': : study-archetype
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.zlcook.studymvn: : com.zlcook.studymvn
.helloword
Confirm properties configuration:
groupId: com.zlcook.studymvn
artifactId: study-archetype
version: 1.0-SNAPSHOT
package: com.zlcook.studymvn.helloword
 Y: : Y
  • 最后输出创建完成信息


    创建完成

查看study-archetype项目

Paste_Image.png

主代码目录src/main/java已经被创建,在该目录下还有一个Java类 com.zlcook.studymvn.helloword.App
测试代码目录也被创建好了,包含一个测试用例 com.zlcook.studymvn.helloword.AppTest。

  • pom.xml文件
    里面添加了一个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>com.zlcook.studymvn</groupId>
  <artifactId>study-archetype</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>study-archetype</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Archetype可以迅速构建一个项目骨架,如果很多项目拥有许多自定义的项目结构以及配置文件,比如A公司要求开放的每个maven项目的目录下都要有readme.txt文件等,则完全可以开发自己的Archetype,然后在这些项目中使用自定义的Archetype来快速生成项目骨架。

6.补充:Maven打包的JAR文件目录内容

通过maven打包好的jar包中包含的目录结构分析:
hibernate-ehcache-3.6.10.Final.jar中的文件


hibernate-ehcache-3.6.10.Final.jar
  • org中时该包中说有源代码被编译后的字节码文件class,这个内容是固定的,不管是使用maven还ant还是gradle构建内容都是一样的。hibernate-ehcache所依赖的包不会被打包进来,他的依赖信息如果是使用maven构建的话会在META-INF中的pom.xml文件中指定。
  • META-INF:打包元数据,里面的内容,和构建工具有一定关系。如果你不是使用maven进行打包的话,那么肯定不会有maven这个文件夹。目录内容如下:
META-INF中文件

MANIFEST.MF文件中内容:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: gbadner
Build-Jdk: 1.5.0_16
Specification-Title: Hibernate Ehcache Integration
Specification-Version: 3.6.10.Final
Specification-Vendor: Hibernate.org
Implementation-Title: Hibernate Ehcache Integration
Implementation-Version: 3.6.10.Final
Implementation-Vendor-Id: org.hibernate
Implementation-Vendor: Hibernate.org
Implementation-URL: http://hibernate.org

maven目录中包含两个文件
org.hibernate/hibernate-ehcache/pom.properties

#Generated by Maven
#Wed Feb 08 19:23:01 CST 2012
version=3.6.10.Final
groupId=org.hibernate
artifactId=hibernate-ehcache

org.hibernate/hibernate-ehcache/pom.xml

<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>org.hibernate</groupId>
        <artifactId>hibernate-parent</artifactId>
        <version>3.6.10.Final</version>
        <relativePath>../hibernate-parent/pom.xml</relativePath>
    </parent>
    
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <packaging>jar</packaging>

    <name>Hibernate Ehcache Integration</name>
    <description>Integration of Hibernate with Ehcache</description>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>hibernate-testing</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容