使用mvn archetype:generate可以创建示例项目,这篇文章maven的基础使相关的命令行创建项目进行简单的示例说明。
环境
liumiaocn:maven liumiao$ mvn -version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /Library/Maven/apache-maven-3.5.4
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"
liumiaocn:maven liumiao$
创建示例项目
liumiaocn:maven liumiao$ ls
liumiaocn:maven liumiao$ mvn archetype:generate -DgroupId=com.liumiao -DartifactId=helloprj
[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 Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
...
2721: remote -> za.co.absa.hyperdrive:component-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1615:
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
7: 1.3
8: 1.4
Choose a number: 8: 8
[INFO] Using property: groupId = com.liumiao
[INFO] Using property: artifactId = helloprj
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = com.liumiao
Confirm properties configuration:
groupId: com.liumiao
artifactId: helloprj
version: 1.0-SNAPSHOT
package: com.liumiao
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.liumiao
[INFO] Parameter: artifactId, Value: helloprj
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.liumiao
[INFO] Parameter: packageInPathFormat, Value: com/liumiao
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.liumiao
[INFO] Parameter: groupId, Value: com.liumiao
[INFO] Parameter: artifactId, Value: helloprj
[INFO] Project created from Archetype in dir: /Users/liumiao/json/json/maven/helloprj
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.686 s
[INFO] Finished at: 2020-06-07T19:43:35+08:00
[INFO] ------------------------------------------------------------------------
liumiaocn:maven liumiao$
目录结构
自动生成的目录结构包括一个单体测试文件、一个示例main文件,一个pom.xml文件,详细如下所示
liumiaocn:maven liumiao$ tree .
.
└── helloprj
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── liumiao
│ └── App.java
└── test
└── java
└── com
└── liumiao
└── AppTest.java
10 directories, 3 files
liumiaocn:maven liumiao$
构建
liumiaocn:maven liumiao$ cd helloprj/
liumiaocn:helloprj liumiao$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.liumiao:helloprj >------------------------
[INFO] Building helloprj 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ helloprj ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloprj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/liumiao/json/json/maven/helloprj/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloprj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/liumiao/json/json/maven/helloprj/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloprj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/liumiao/json/json/maven/helloprj/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloprj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/liumiao/json/json/maven/helloprj/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloprj ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.liumiao.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 s - in com.liumiao.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ helloprj ---
[INFO] Building jar: /Users/liumiao/json/json/maven/helloprj/target/helloprj-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.084 s
[INFO] Finished at: 2020-06-07T19:47:25+08:00
[INFO] ------------------------------------------------------------------------
liumiaocn:helloprj liumiao$
执行
liumiaocn:helloprj liumiao$ java -jar target/helloprj-1.0-SNAPSHOT.jar
no main manifest attribute, in target/helloprj-1.0-SNAPSHOT.jar
liumiaocn:helloprj liumiao$
提示出现没有main的设定,有多种对应方法,比如执行时指定:
liumiaocn:helloprj liumiao$ java -cp target/helloprj-1.0-SNAPSHOT.jar com.liumiao.App
Hello World!
liumiaocn:helloprj liumiao$
或者修改pom.xml配置文件,当前自动生成的pom文件内容如下所示:
liumiaocn:helloprj liumiao$ cat 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.liumiao</groupId>
<artifactId>helloprj</artifactId>
<version>1.0-SNAPSHOT</version>
<name>helloprj</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>
liumiaocn:helloprj liumiao$
将打jar包部分的设定做如下修改,打出的jar包中自然就会找到main函数了
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.liumiao.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
执行结果如下所示
liumiaocn:helloprj liumiao$ java -jar target/helloprj-1.0-SNAPSHOT.jar
Hello World!
liumiaocn:helloprj liumiao$
转载:https://blog.csdn.net/liumiaocn/article/details/106606177
查看评论