Maven使用之三-使用Maven命令创建Java项目
2021-01-07
Maven使用之三-使用Maven命令创建Java项目
Maven是Java语言编写的,必须先配置Java环境,之后配置Maven环境。
Maven的一个重要作用就是统一管理jar包,为了一个项目可以build或运行,项目中不可避免的,会依赖很多其他的jar包,在Maven中,这些依赖就被称为dependency。
生成maven项目的原理是,依赖一个插件maven-archetype-plugin,然后这个插件自带一些archetype模版,也可以说成项目的骨架。
创建命令:mvn -Dxxx=yyy 指定Java全局属性;
这里我的目录D:\Projects\java\MvnProject,在该目录下执行
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=com.mycompany.mypro -DartifactId=my-project -Dversion=0.1 -DinteractiveMode=false
或者
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=com.mycompany.mypro -DartifactId=my-project -Dversion=0.1 -DinteractiveMode=false -DarchetypeCatalog=local
命令说明:生成maven项目的原理是,依赖一个插件maven-archetype-plugin,然后这个插件自带一些archetype模版,也可以说成项目的骨架。
DarchetypeGroupId指定了用那个模版(骨架)的GroupId
DarchetypeArtifactId是指定了用那个模版(骨架)ArtifactId
当它等于maven-archetype-quickstart,创建一个简单的 Java 应用;maven-archetype-webapp,创建一个Web Project
DarchetypeVersion是指定了用那个模版(骨架)的版本
DgroupId 组织标识(包名),代表的是唯一的一个标识,比如一个组织或者一个项目的名字,基本上是唯一的,以这个maven为例就是org.apache.maven。
DartifactId 项目名称,代表的是名字,如果刚刚那个有点像身份证,这个就是类似姓名的东西。
Dversion 就是版本号,比如1.2.1版本这样的。
DinteractiveMode 是否使用交互模式
结果如下所示:
[D:\Projects\java\MvnProject]$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=com.mycompany.mypro -DartifactId=my-project -Dversion=0.1 -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.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.1.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.1.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [WARNING] No archetype found in remote catalog. Defaulting to internal catalog [INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-quickstart:1.1] found in catalog internal [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.mycompany.mypro [INFO] Parameter: artifactId, Value: my-project [INFO] Parameter: version, Value: 0.1 [INFO] Parameter: package, Value: com.mycompany.mypro [INFO] Parameter: packageInPathFormat, Value: com/mycompany/mypro [INFO] Parameter: package, Value: com.mycompany.mypro [INFO] Parameter: version, Value: 0.1 [INFO] Parameter: groupId, Value: com.mycompany.mypro [INFO] Parameter: artifactId, Value: my-project [INFO] Project created from Archetype in dir: D:\Projects\java\MvnProject\my-project [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.215 s [INFO] Finished at: 2021-01-07T15:38:58+08:00 [INFO] ------------------------------------------------------------------------
生成的目录结构及内容:
my-project(项目根目录) |--src | |--main(主目录) | | |--java(存放项目的.java文件) | | | |--com | | | | |--mycompany | | | | | |--mypro | | | | | | |--App.java | |--test(测试代码目录) | | |--java | | | |--com | | | | |--mycompany | | | | | |--mypro | | | | | | |--AppTest.java |--pom.xml(用于标识该项目是一个Maven项目)
其中App.java如下:
package com.mycompany.mypro; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
AppTest.java如下:
package com.mycompany.mypro; import static org.junit.Assert.assertTrue; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { /** * Rigorous Test :-) */ @Test public void shouldAnswerWithTrue() { assertTrue( true ); } }
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.mypro</groupId> <artifactId>my-project</artifactId> <version>0.1</version> <name>my-project</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
我自己新加了两个文件:myproject.txt myproject.bat
myproject.txt 项目的说明文件
myproject.bat 项目的执行脚本
myproject.bat 如下:
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' @echo off @REM set title of command window title %0 @REM 当前脚本文件所在的路径 set CurProPath=%~dp0 @REM 设置 java文件目录 set JavaPath=%CurProPath%src\main\java @REM 设置 java文件测试目录 set JavaTestPath=%CurProPath%src\test\java @REM 设置资源目录 set JavaResourcePath=%JavaPath%resources @REM 设置 class 目录 set JavaClassPath=%CurProPath%target\classes @REM 设置基础包名 set GroupIdPath=\com\mycompany\mypro set GroupId=com.mycompany.mypro @setlocal set ERROR_CODE=0 @REM To isolate internal variables from possible post scripts, we use another setlocal @setlocal @REM ==== START VALIDATION ==== if not "%JAVA_HOME%" == "" goto OkJHome echo. echo Error: JAVA_HOME not found in your environment. >&2 echo Please set the JAVA_HOME variable in your environment to match the >&2 echo location of your Java installation. >&2 echo. goto error :OkJHome if exist "%JAVA_HOME%\bin\java.exe" goto OkMHome echo. echo Error: JAVA_HOME is set to an invalid directory. >&2 echo JAVA_HOME = "%JAVA_HOME%" >&2 echo Please set the JAVA_HOME variable in your environment to match the >&2 echo location of your Java installation. >&2 echo. goto error :OkMHome set MAVEN_HOME=E:\Programs\maven\apache-maven-3.6.3 if exist "%MAVEN_HOME%\bin\mvn.cmd" goto init echo. echo Error: MAVEN_HOME is set to an invalid directory. >&2 echo MAVEN_HOME = "%MAVEN_HOME%" >&2 echo Please set the MAVEN_HOME variable in your environment to match the >&2 echo location of your Maven installation. >&2 echo. goto error @REM ==== END VALIDATION ==== :init call %MAVEN_HOME%\bin\mvn validate compile test @REM call %MAVEN_HOME%\bin\mvn compile @REM call %MAVEN_HOME%\bin\mvn test set lib=.;target\classes @REM %JAVA_HOME%\bin\java -classpath target\classes %GroupId%.App %JAVA_HOME%\bin\java -classpath %lib% %GroupId%.App exit @pause
命令行中执行脚本:
先切换到该项目的目录下,该目录有pom.xml
.\myproject.bat
执行结果如下:
生成的结构如下: