在上文中(https://blog.csdn.net/solinger/article/details/100935057),我们没有使用模版,而直接使用maven对helloworld项目进行了管理和构建。
本文中,我们将使用maven模版达到我们的目标。
[wlin@wlin ~]$ mvn archetype:generate -DgroupId=com.example.cara -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/wlin
[INFO] Parameter: package, Value: com.example.cara
[INFO] Parameter: groupId, Value: com.example.cara
[INFO] Parameter: artifactId, Value: HelloWorld
[INFO] Parameter: packageName, Value: com.example.cara
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/wlin/HelloWorld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.791 s
[INFO] Finished at: 2019-09-17T19:37:03+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin ~]$ cd HelloWorld/
[wlin@wlin HelloWorld]$ tree
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── cara
│ └── App.java
└── test
└── java
└── com
└── example
└── cara
└── AppTest.java
11 directories, 3 files
其中:-DgroupId用来指定package名字,-DartifactId用来指定project名字
则我们可以看到,源代码放在src/main/java目录下,测试代码放在src/test/java/。这是符合maven目录标准布局的。
也是我们为什么不使用模版也这样放置代码的原因。更多目录标准,可见:
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
且在这一步中生成了一个标准的pom.xml,定义目录结构,项目插件,项目依赖,如何构建项目等。关于pom, 可参考:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
查看生成的默认文件
[wlin@wlin HelloWorld]$ cat src/main/java/com/example/cara/App.java
package com.example.cara;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
则就是我们希望的hello world文件,这里不进行改动。
则让我们尝试相关mvn指令
[wlin@wlin HelloWorld]$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:HelloWorld >---------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wlin/HelloWorld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/wlin/HelloWorld/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.847 s
[INFO] Finished at: 2019-09-17T19:44:20+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin HelloWorld]$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:HelloWorld >---------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wlin/HelloWorld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wlin/HelloWorld/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloWorld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/wlin/HelloWorld/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloWorld ---
[INFO] Surefire report directory: /home/wlin/HelloWorld/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.cara.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.211 s
[INFO] Finished at: 2019-09-17T19:44:29+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin HelloWorld]$ mvn exec:java -Dexec.mainClass=com.example.cara.App
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:HelloWorld >---------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ HelloWorld ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.520 s
[INFO] Finished at: 2019-09-17T19:46:31+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin HelloWorld]$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:HelloWorld >---------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wlin/HelloWorld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wlin/HelloWorld/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloWorld ---
[INFO] Surefire report directory: /home/wlin/HelloWorld/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.cara.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloWorld ---
[INFO] Building jar: /home/wlin/HelloWorld/target/HelloWorld-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.094 s
[INFO] Finished at: 2019-09-17T19:46:47+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin HelloWorld]$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:HelloWorld >---------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloWorld ---
[INFO] Deleting /home/wlin/HelloWorld/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.208 s
[INFO] Finished at: 2019-09-17T19:47:03+08:00
[INFO] ------------------------------------------------------------------------
本文介绍如何利用Maven模板来创建和管理Java HelloWorld项目,通过-DgroupId和-DartifactId指定项目信息,遵循Maven的标准目录结构。生成的pom.xml文件定义了项目配置、插件和依赖。默认生成的Hello World文件无需改动,可以使用相关Maven指令进行操作。

1826

被折叠的 条评论
为什么被折叠?



