Maven 自定义项目骨架
1. 创建pom.xml
<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>archetype.groupId</groupId> <artifactId>simple-archetype</artifactId> <version>1.0</version> <packaging>jar</packaging> </project>
这个pom文件中定义的元素标签与mvn archetype:generate 可选参数相对应。
pom.xml中元素标签 | mvn archetype:generate 可选参数 |
archifactId | archetypeArtifactId |
groupId | archetypeGroupId |
version | archetypeVersion |
2. 创建archetype.xml
<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd"> <id>simple-archetype</id> <sources> <source>src/main/java/App.java</source> </sources> <testSources> <source>src/test/java/AppTest.java</source> </testSources> </archetype>
archetype.xml 是一个文件组织架构描述文件。
- <sources> = src/main/java
- <resources> = src/main/resources
- <testSources> = src/test/java
- <testResources> = src/test/resources
- <siteResources> = src/site
3. 创建原型pom.xml。
<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>${groupId}</groupId> <artifactId>${artifactId}</artifactId> <version>${version}</version> <packaging>jar</packaging> <name>A custom project</name> <url>http://www.myorganization.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
这个pom文件中定义的元素标签与mvn archetype:generate 可选参数相对应。这个pom定义了要生成的文件架构的archefact,group id 和 version架构。
pom.xml中元素标签 | mvn archetype:generate 可选参数 |
archifactId | archifactId |
groupId | groupId |
version | version |
4. 创建App.java 和 AppTest.java.
App.java
package com.mycompany.app; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
AppTest.java
package com.mycompany.app; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } }
5. 创建如下文件结构
6. 打开1中pom.xml所在目录。 执行mvn install 进行安装.
7. 创建一个新目录。执行以下命令
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DinteractiveMode=false -DarchetypeArtifactId=simple-archetype -DarchetypeGroupId=archetype.groupId -DarchetypeVersion=1.0 -DarchetypeCatalog=local
就可以查看到生成的文件骨架了。
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01