您好,登錄后才能下訂單哦!
Maven自身提供了許多Archetype來方便用戶創建Project,但是每個團隊都可能會有一些常用的文件或配置,為了避免在創建project時重復的拷貝和修改,我們通過自定義Archetype來規范顯得還蠻有必要,下面簡單介紹下使用maven-archetype-archetype來構建自己的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>
如:
<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>
例如:
如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);
}
}
如:
<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>
mvn install
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
<?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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。