Maven自身提供了许多Archetype来方便用户创建Project,但是每个团队都可能会有一些常用的文件或配置,为了避免在创建project时重复的拷贝和修改,我们通过自定义Archetype来规范显得还蛮有必要,下面简单介绍下使用maven-archetype-archetype来构建自己的Archetype的过程。
1,创建自己的Archetype项目
模板
mvn archetype:generate /
-DarchetypeGroupId=<archetype-groupId> /
-DarchetypeArtifactId=<archetype-artifactId> /
-DarchetypeVersion=<archetype-version> /
-DgroupId=<my.groupid> /
-DartifactId=<my-artifactId>
例如:
mvn archetype:generate /
-DgroupId=com.ultrapower.maven.archetypes /
-DartifactId=springboot-maven-archetype /
-DarchetypeArtifactId=maven-archetype-archetype /
-DinteractiveMode=false /
-X
1) springboot-maven-archetype下的pom.xml和一般的maven项目一样用于定义archetype项目的坐标等信息
2) springboot-maven-archetype/src/main/resources/archetype-resources下的所有内容定义了待生成项目的所有文件(原型文件)
3) springboot-maven-archetype/ src/main/resources/META-INF/maven/archetype.xml中定义骨架的描述符(元数据),这个文件列出了包含在archetype中的所有文件并将这些文件分类。
注:springboot-maven-archetype/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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ultrapower.maven.archetypes</groupId>
<artifactId>springboot-maven-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - springboot-maven-archetype</name>
<url>http://maven.apache.org</url>
</project>
2,修改archetype-resources下的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/maven-v4_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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3,添加需要的文件
例如:
如Application.java
package ${package};
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
JmsAutoConfiguration.class })
@ServletComponentScan
@ComponentScan(basePackages = { "${package}" })
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
如CorsConfig.java
package ${package}.config.beanconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 1 设置访问源地址
corsConfiguration.addAllowedOrigin("*");
// 2 设置访问源请求头
corsConfiguration.addAllowedHeader("*");
// 3 设置访问源请求方法
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// 4 对接口配置跨域设置
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
4,修改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>springboot-maven-archetype</id>
<sources>
<source encoding="UTF-8">src/main/java/Application.java</source>
<source encoding="UTF-8">src/main/java/config/beanconfig/MvcConfig.java</source>
<source encoding="UTF-8">src/main/java/config/beanconfig/CorsConfig.java</source>
<source encoding="UTF-8">src/main/java/config/beanconfig/RestTemplateConfig.java</source>
<source encoding="UTF-8">src/main/java/config/beanconfig/Swagger2Config.java</source>
<source encoding="UTF-8">src/main/java/config/condition/Swagger2Condition.java</source>
</sources>
<resources>
<resource encoding="UTF-8">src/main/resources/application.yml</resource>
<resource encoding="UTF-8">src/main/resources/application-dev.yml</resource>
<resource encoding="UTF-8">src/main/resources/application-prod.yml</resource>
<resource encoding="UTF-8">boot/control.sh</resource>
<resource encoding="UTF-8">boot/control.bat</resource>
</resources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>
上面是将每个文件都列出来了,还可以使用通配符的方式来简化配置,例如:
<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>springboot-maven-archetype</id>
<sources>
<source encoding="UTF-8">src/main/java/**/*.*</source>
</sources>
<resources>
<resource encoding="UTF-8">**/*.*</resource>
</resources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>
5,安装archetype
mvn install
6,使用自定义的archetype创建项目
mvn archetype:generate -DarchetypeGroupId=com.ultrapower.maven.archetypes -DarchetypeArtifactId=springboot-maven-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.ultrapower.ioss -DartifactId=springboot-archetype-test -X
注意: 我们可以将archetype.xml换成“archetype-metadata.xml”,以下的内容可以达到同样的效果,但还是推荐archetype.xml
archetype-metadata.xml:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="beast-archetype"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<requiredProperties>
<requiredProperty key="package" />
</requiredProperties>
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>boot</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/192802.html