在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。