[Maven]自定义Archetypes
Archetypes就相当于脚手架,如果在平常项目中,有个基础的模版,你就可以直接基于模版构建项目,
1. 新建一个maven项目,定义项目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>com.ppdai.cbd.ddp</groupId> <artifactId>flinkCustomArchitype</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-archetype</packaging> <build> <extensions> <extension> <groupId>org.apache.maven.archetype</groupId> <artifactId>archetype-packaging</artifactId> <version>3.1.1</version> </extension> </extensions> </build> </project>
2.创建 archetype描述文件,文件所在目录 (项目根目录)flinkCustomArchitype/src/main/resources/META-INF/maven
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="flinkCustomArchitype"> <fileSets> <fileSet filtered="true" packaged="true"> <directory>src/main/java</directory> </fileSet> </fileSets> </archetype-descriptor>
3. 目录结构
archetype |-- pom.xml `-- src `-- main `-- resources |-- META-INF | `-- maven | `--archetype-metadata.xml `-- archetype-resources |-- pom.xml (prototype pom.xml)
`-- src
|-- main
| `-- java
| `-- App.java
`-- test
`-- java
`-- AppTest.java
4. 创建 prototype 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>${artifactId}</name> <url>http://www.myorganization.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
5. mvn install 本地安装
6. cd 一个空目录,执行
mvn archetype:generate \ -DarchetypeGroupId=<archetype-groupId> \ -DarchetypeArtifactId=<archetype-artifactId> \ -DarchetypeVersion=<archetype-version> \ -DgroupId=<my.groupid> \ -DartifactId=<my-artifactId>
完结。 又水了一篇文章
欢迎关注Java流水账公众号