使用Maven原型创建Java项目
创建Maven项目的方法有很多。
我们可以使用流行的IDE(例如Eclipse和IntelliJ IDEA)中的内置插件。
我们还可以从命令行创建一个maven项目。
在本教程中,我们将学习如何通过命令行使用Maven原型创建一个简单的Java项目。
Maven命令创建Java项目
首先,创建要其中创建Java项目的目录。
$mkdir maven-examples $cd maven-examples
然后,运行以下命令创建一个简单的Java项目。
$mvn archetype:generate -DgroupId=com.theitroad.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.theitroad.java [INFO] Using property: artifactId = java-app Define value for property 'version' 1.0-SNAPSHOT: : [INFO] Using property: package = com.theitroad.java Confirm properties configuration: groupId: com.theitroad.java artifactId: java-app version: 1.0-SNAPSHOT package: com.theitroad.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.theitroad.java [INFO] Parameter: groupId, Value: com.theitroad.java [INFO] Parameter: artifactId, Value: java-app [INFO] Parameter: packageName, Value: com.theitroad.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] -----------------------------------------------------------------------
如果查看输出,则行家正在确认项目配置。
我们可以使用-DinteractiveMode = false参数来跳过它。
现在,让我们尝试了解此命令的幕后情况。
原型是一个Maven项目模板工具包。
这是从命令行创建Maven项目的好方法。
archetype:generate告诉maven我们必须从命令中提供的附加信息中生成一个maven项目。每个Maven项目都需要一些特定的配置,例如groupId,artifactId,打包,版本和名称。
-DgroupId = com.theitroad.java
指定我们正在创建的项目的groupId。类似地,
-DartifactId = java-app
指定项目的artifactId。-DarchetypeArtifactId = maven-archetype-quickstart
指定用于项目的模板。
" maven-archetype-quickstart"工件会生成一个简单的基于JAR的maven项目。
项目的版本设置为1.0-SNAPSHOT。
Maven Java项目的目录结构
我们可以在Linux系统中使用tree
命令来获取目录层次结构。
$ls java-app $cd java-app $tree . ├── pom.xml └── src ├── main │ └── java │ └── com │ └── theitroad │ └── java │ └── App.java └── test └── java └── com └── theitroad └── java └── AppTest.java 11 directories, 3 files $
注意,maven原型已经创建了一个简单的Java类和一个JUnit测试用例。
我们可以扩展该项目以为其添加更多功能。
App.java类具有以下代码。
package com.theitroad.java; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
它具有main方法,因此我们可以从maven项目构建JAR之后执行此类。
建立Maven项目
让我们尝试使用mvn package
命令来构建Maven项目。
$mvn package [INFO] Scanning for projects... [INFO] [INFO] --------------------< com.theitroad.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 $
Maven构建失败,因为它使用的是不支持最新版本的旧版本的" maven-compiler-plugin"。
我们可以通过在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>
这告诉maven使用maven-compiler-plugin
版本3.8.1和Java 13版本来编译项目。
现在,让我们再次构建项目。
$mvn package [INFO] Scanning for projects... [INFO] [INFO] --------------------< com.theitroad.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.theitroad.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] ----------------------------------------------------------------------- $
首先,从maven中央存储库下载所有必需的JAR。
然后,编译项目代码并执行测试用例。
最后,构建JAR,并在其位置打印" BUILD SUCCESS"消息。
如果要跳过执行测试用例,请将-Dmaven.test.skip = true
参数传递给Maven。
运行Maven项目JAR
让我们从项目JAR文件中运行App
类。
$cd target $java -cp java-app-1.0-SNAPSHOT.jar com.theitroad.java.App Hello World!
我们还可以在JAR列表文件中设置Main-Class参数。
如果解压缩JAR文件,您会发现文件名为META-INF/MANIFEST.MF。
该文件包含项目信息,由maven自动生成。
我们可以通过在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.theitroad.java.App</mainClass> </manifest> </archive> </configuration> </plugin>
如果使用此配置构建maven项目,则列表文件中将为主类提供一个新配置。
Main-Class: com.theitroad.java.App
现在,我们可以使用java -jar命令运行JAR文件,它将寻找Main-Class参数来运行该类。
$java -jar target/java-app-1.0-SNAPSHOT.jar Hello World!