您好,登錄后才能下訂單哦!
在Maven項目中,自定義Maven Archetype可以讓你快速生成項目結構。以下是創建自定義Maven Archetype的步驟:
<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.example</groupId>
<artifactId>custom-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<properties>
<archetypeArtifactId>custom-archetype</archetypeArtifactId>
<archetypeVersion>1.0-SNAPSHOT</archetypeVersion>
<maven-archetype-plugin.version>3.2.0</maven-archetype-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven-archetype-plugin.version}</version>
<executions>
<execution>
<id>attach-archetypes</id>
<configuration>
<archetypes>
<archetype>${archetypeArtifactId}:${archetypeVersion}</archetype>
</archetypes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/resources/archetype-metadata.xml
文件中定義Archetype的元數據。這個文件描述了Archetype的結構和配置選項。例如:<archetypeMetadata xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-metadata/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-metadata/1.1.0 http://maven.apache.org/xsd/archetype-metadata-1.1.0.xsd">
<archetypeArtifactId>custom-archetype</archetypeArtifactId>
<archetypeVersion>1.0-SNAPSHOT</archetypeVersion>
<description>A custom Maven archetype</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<fileSets>
<fileSet>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet>
<directory>src/test/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
</fileSets>
</archetypeMetadata>
src/main/resources
目錄下創建Archetype的模板文件。例如,你可以創建一個簡單的Java類模板:package ${package};
public class ${artifactId} {
public static void main(String[] args) {
System.out.println("Hello, ${artifactId}!");
}
}
mvn clean install
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=custom-archetype -DarchetypeVersion=1.0-SNAPSHOT
這將會根據你的自定義Archetype生成一個新的Maven項目。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。