There are many ways to create a maven project. We can use built-in plugins from the popular IDEs such as Eclipse and IntelliJ IDEA. We can also create a maven project from the command line. In this tutorial, we will learn how to create a simple Java project using Maven archetypes through the command line.
创建Maven项目的方法有很多。 我们可以使用流行的IDE(例如Eclipse和IntelliJ IDEA)中的内置插件。 我们还可以从命令行创建一个maven项目。 在本教程中,我们将学习如何通过命令行使用Maven原型创建一个简单的Java项目。
Maven命令创建Java项目 (Maven Command to Create a Java Project)
First of all, create a directory where you want to create the java project.
首先,创建要在其中创建Java项目的目录。
$ mkdir maven-examples
$ cd maven-examples
Then, run the following command to create a simple java project.
然后,运行以下命令来创建一个简单的Java项目。
$ mvn archetype:generate -DgroupId=com.journaldev.java -DartifactId=java-app -DarchetypeArtifactId=maven-archetype-quickstart
[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.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom (1.5 kB at 2.8 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom (1.3 kB at 2.5 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (4.3 kB at 7.9 kB/s)
[INFO] Using property: groupId = com.journaldev.java
[INFO] Using property: artifactId = java-app
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = com.journaldev.java
Confirm properties configuration:
groupId: com.journaldev.java
artifactId: java-app
version: 1.0-SNAPSHOT
package: com.journaldev.java
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/pankaj/Desktop/maven-examples
[INFO] Parameter: package, Value: com.journaldev.java
[INFO] Parameter: groupId, Value: com.journaldev.java
[INFO] Parameter: artifactId, Value: java-app
[INFO] Parameter: packageName, Value: com.journaldev.java
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/pankaj/Desktop/maven-examples/java-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.103 s
[INFO] Finished at: 2019-12-05T13:23:58+05:30
[INFO] ------------------------------------------------------------------------
If you look at the output, the maven is confirming project configurations. We can skip this using the -DinteractiveMode=false argument.
如果查看输出,则行家正在确认项目配置。 我们可以使用-DinteractiveMode=false参数跳过此-DinteractiveMode=false 。
Now, let's try to understand what is happening behind the scene of this command.
现在,让我们尝试了解此命令的幕后情况。
- Archetype is a maven project templating toolkit. It's a great way to create maven projects from the command line. The
archetype:generatetells maven that we have to generate a maven project from the additional information provided in the command. 原型是一个Maven项目模板工具包。 这是从命令行创建Maven项目的好方法。archetype:generate告诉maven我们必须根据命令中提供的附加信息来生成maven项目。 - Every maven project requires some specific configurations, such as groupId, artifactId, packaging, version, and name. The
-DgroupId=com.journaldev.javaspecifies the groupId of the project we are creating. 每个Maven项目都需要一些特定的配置,例如groupId,artifactId,打包,版本和名称。-DgroupId=com.journaldev.java指定我们正在创建的项目的groupId。 - Similarly,
-DartifactId=java-appspecify the artifactId of the project. 同样,-DartifactId=java-app指定项目的artifactId。 - The
-DarchetypeArtifactId=maven-archetype-quickstartspecify the template to use for the project. Themaven-archetype-quickstartartifact generates a simple JAR based maven project. The version of the project is set to 1.0-SNAPSHOT.-DarchetypeArtifactId=maven-archetype-quickstart指定用于项目的模板。maven-archetype-quickstart工件会生成一个简单的基于JAR的maven项目。 项目的版本设置为1.0-SNAPSHOT。
Maven Java项目的目录结构 (Directory Structure of the Maven Java Project)
We can use tree command in Linux systems to get the directory hierarchy.
我们可以在Linux系统中使用tree命令来获取目录层次结构。
$ ls
java-app
$ cd java-app
$ tree
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── journaldev
│ └── java
│ └── App.java
└── test
└── java
└── com
└── journaldev
└── java
└── AppTest.java
11 directories, 3 files
$
Notice that the maven archetype has created a simple Java class and a JUnit test case for it. We can extend this project to add more functionalities to it. The App.java class has the following code.
注意,maven原型已经创建了一个简单的Java类和一个JUnit测试用例 。 我们可以扩展该项目以为其添加更多功能。 App.java类具有以下代码。
package com.journaldev.java;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
It has a main method, so we can execute this class after building the JAR from the maven project.
它具有main方法 ,因此我们可以从maven项目构建JAR之后执行此类。
建立Maven项目 (Building the Maven Project)
Let's try to build the maven project using the mvn package command.
让我们尝试使用mvn package命令来构建maven项目。
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.journaldev.java:java-app >--------------------
[INFO] Building java-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pankaj/Desktop/maven-examples/java-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-app ---
[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 /Users/pankaj/Desktop/maven-examples/java-app/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.919 s
[INFO] Finished at: 2019-12-05T13:26:17+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project java-app: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] https://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
$
The maven build failed because it's using an older version of maven-compiler-plugin that doesn't support the latest version.
Maven构建失败,因为它使用的maven-compiler-plugin的旧版本不支持最新版本。
We can fix this issue by adding below configurations to the pom.xml file.
我们可以通过在pom.xml文件中添加以下配置来解决此问题。
<properties>
<maven.compiler.release>13</maven.compiler.release>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
This tells maven to use maven-compiler-plugin version 3.8.1 and Java 13 release to compile the project.
这告诉maven使用maven-compiler-plugin版本3.8.1和Java 13版本来编译项目。
Now, let's build the project once again.
现在,让我们再次构建项目。
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.journaldev.java:java-app >--------------------
[INFO] Building java-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.pom (12 kB at 7.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/33/maven-plugins-33.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/33/maven-plugins-33.pom (11 kB at 20 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/33/maven-parent-33.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/33/maven-parent-33.pom (44 kB at 56 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/apache/21/apache-21.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/apache/21/apache-21.pom (17 kB at 32 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar (62 kB at 115 kB/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pankaj/Desktop/maven-examples/java-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ java-app ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.pom (5.6 kB at 10 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.pom (5.1 kB at 8.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-languages/0.9.10/plexus-languages-0.9.10.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-languages/0.9.10/plexus-languages-0.9.10.pom (4.1 kB at 7.9 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/6.2/asm-6.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/6.2/asm-6.2.pom (2.9 kB at 5.4 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.pom (16 kB at 30 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.pom (867 B at 1.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler/2.8.4/plexus-compiler-2.8.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler/2.8.4/plexus-compiler-2.8.4.pom (6.0 kB at 12 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.pom (692 B at 1.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.pom (771 B at 1.5 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compilers/2.8.4/plexus-compilers-2.8.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compilers/2.8.4/plexus-compilers-2.8.4.pom (1.3 kB at 2.5 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/6.2/asm-6.2.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar
Downloading from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M9/qdox-2.0-M9.jar (317 kB at 269 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar (39 kB at 26 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar (27 kB at 17 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar (4.7 kB at 2.6 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.jar (167 kB at 82 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/6.2/asm-6.2.jar (111 kB at 54 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar (21 kB at 10 kB/s)
[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 /Users/pankaj/Desktop/maven-examples/java-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pankaj/Desktop/maven-examples/java-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ java-app ---
[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 /Users/pankaj/Desktop/maven-examples/java-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-app ---
[INFO] Surefire report directory: /Users/pankaj/Desktop/maven-examples/java-app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.journaldev.java.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-app ---
[INFO] Building jar: /Users/pankaj/Desktop/maven-examples/java-app/target/java-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.742 s
[INFO] Finished at: 2019-12-05T13:29:15+05:30
[INFO] ------------------------------------------------------------------------
$
First of all, all the required JARs are downloaded from the maven central repository.
首先,从maven中央存储库下载所有必需的JAR。
Then, the project code is compiled and test cases are executed.
然后,编译项目代码并执行测试用例。
Finally, the JAR is built and its location is printed with the BUILD SUCCESS message.
最后,将构建JAR,并使用BUILD SUCCESS消息打印其位置。
-Dmaven.test.skip=true argument to the maven.
-Dmaven.test.skip=true参数传递给Maven。
运行Maven项目JAR (Running the Maven Project JAR)
Let's run the App class from the project JAR file.
让我们从项目JAR文件中运行App类。
$ cd target
$ java -cp java-app-1.0-SNAPSHOT.jar com.journaldev.java.App
Hello World!
We can also set the Main-Class parameter in the JAR manifest file. If you unpack the JAR file, you will notice that there is a file META-INF/MANIFEST.MF. This file contains the project information and it's auto-generated by maven.
我们还可以在JAR清单文件中设置Main-Class参数。 如果解压缩JAR文件,您会注意到文件META-INF/MANIFEST.MF 。 该文件包含项目信息,由maven自动生成。
We can add the Main-Class parameter to it by adding the following plugin configuration in the pom.xml file.
我们可以通过在pom.xml文件中添加以下插件配置来为其添加Main-Class参数。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.journaldev.java.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If you build the maven project with this configuration, there will be a new configuration in the manifest file for the main class.
如果使用此配置构建maven项目,则清单文件中将为主类提供一个新配置。
Main-Class: com.journaldev.java.App
Now, we can run the JAR file using java -jar command and it will look for the Main-Class parameter to run the class.
现在,我们可以使用java -jar命令运行JAR文件,它将寻找Main-Class参数来运行该类。
$ java -jar target/java-app-1.0-SNAPSHOT.jar
Hello World!
结论 (Conclusion)
We learned how to create a maven project using archetypes. It's a simple and effective way to create a starter project.
我们学习了如何使用原型创建Maven项目。 这是创建入门项目的简单有效的方法。
参考资料 (References)
翻译自: https://www.journaldev.com/33593/creating-java-project-maven-archetypes
本文详细介绍了如何使用Maven原型从命令行创建Java项目,包括创建项目的基本步骤、配置参数、目录结构解析,以及如何构建和运行项目。

2958

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



