溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

自定義Maven Archetype之 maven-archetype-archetype

發(fā)布時(shí)間:2020-07-03 15:39:47 來源:網(wǎng)絡(luò) 閱讀:2013 作者:mybabe0312 欄目:軟件技術(shù)

Maven自身提供了許多Archetype來方便用戶創(chuàng)建Project,但是每個(gè)團(tuán)隊(duì)都可能會有一些常用的文件或配置,為了避免在創(chuàng)建project時(shí)重復(fù)的拷貝和修改,我們通過自定義Archetype來規(guī)范顯得還蠻有必要,下面簡單介紹下使用maven-archetype-archetype來構(gòu)建自己的Archetype的過程。

1,創(chuàng)建自己的Archetype項(xiàng)目

模板

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

自定義Maven Archetype之 maven-archetype-archetype

1) springboot-maven-archetype下的pom.xml和一般的maven項(xiàng)目一樣用于定義archetype項(xiàng)目的坐標(biāo)等信息
2) springboot-maven-archetype/src/main/resources/archetype-resources下的所有內(nèi)容定義了待生成項(xiàng)目的所有文件(原型文件)
3) springboot-maven-archetype/ src/main/resources/META-INF/maven/archetype.xml中定義骨架的描述符(元數(shù)據(jù)),這個(gè)文件列出了包含在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,添加需要的文件

例如:
自定義Maven Archetype之 maven-archetype-archetype

如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 設(shè)置訪問源地址
        corsConfiguration.addAllowedOrigin("*");
        // 2 設(shè)置訪問源請求頭
        corsConfiguration.addAllowedHeader("*");
        // 3 設(shè)置訪問源請求方法
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 4 對接口配置跨域設(shè)置
        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>

上面是將每個(gè)文件都列出來了,還可以使用通配符的方式來簡化配置,例如:

<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創(chuàng)建項(xiàng)目

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”,以下的內(nèi)容可以達(dá)到同樣的效果,但還是推薦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>
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI