骨架

maven骨架是一种快速生成模板项目的方法,这里记录创建、使用的过程

step1 从模板中骨架抽离

首先我们可以使用以下命令对一个模板项目进行骨架抽离工作。
所谓骨架抽离,其实就是将包名、groupId、artifactId、version参数化,抽离出一个用占位符号代替的工程。
mvn archetype:create-from-project

5136793-aa94461af3788497.png
演示目录

该命令会将以上内容存入target.generated-sources.archetype目录中

5136793-411217b9bb8a465f.png
产生抽离工程目录结构

这里有多个pom文件

  • 第一个pom文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<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.jikee.template</groupId>
  <artifactId>template-boot-single-archetype</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>maven-archetype</packaging>

  <name>template-boot-single-archetype</name>

  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.0.1</version>
      </extension>
    </extensions>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.0.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <description>Parent pom providing dependency and plugin management for applications
        built with Maven</description>

  <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/template-boot-single</url>

  <developers>
    <developer>
      <name>Pivotal</name>
      <email>[email protected]</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>http://www.spring.io</organizationUrl>
    </developer>
  </developers>

  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>

  <scm>
    <url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/template-boot-single</url>
  </scm>
</project>

这里注意下
<packaging>maven-archetype</packaging>
可以看到这个文件是用来做骨架的pom文件,而不是原文件

  • 第二个pom文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot.version>2.0.2.RELEASE</spring-boot.version>
        <mybatis-starter.version>1.3.1</mybatis-starter.version>
        <druid-starter.version>1.1.10</druid-starter.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.8</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <url>http://frps:8081/nexus/content/repositories/releases/</url>
        </repository>
    </repositories>
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://frps:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://frps:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>

    </distributionManagement>
    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.archetype</groupId>
                <artifactId>archetype-packaging</artifactId>
                <version>3.0.1</version>
            </extension>
        </extensions>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>3.0.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

注意这里

   <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>

可以看到一件被抽离了

step2 安装到本地 或者发布到远程

在target.generated-sources.archetype目录中使用以下命令 安装到本地和发布到远程
mvn archetype:install
mvn deploy
注意 mvn deploy 需要在target.generated-sources.archetype.pom.xml文件中指定发布源

<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://frps:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://frps:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>

    </distributionManagement>

step3 使用

在任意位置 使用 mvn archetype:generate来弹出创建目录,选择刚刚建立的模板,输入提示的包名、groupId、artifactId、version信息即可创建

goodluck

扩展

元数据配置文件archetype-metadata.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" name="template-boot-single"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <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>**/*.xml</include>
        <include>**/*.html</include>
      </includes>
    </fileSet>
    <fileSet encoding="UTF-8">
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.yml</include>
      </includes>
    </fileSet>
    <fileSet encoding="UTF-8">
      <directory></directory>
      <includes>
        <include>README.md</include>
        <include>.gitignore</include>
      </includes>
    </fileSet>
  </fileSets>
</archetype-descriptor>

扩展阅读

https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
https://www.cnblogs.com/wdas-87895/p/6290912.html

猜你喜欢

转载自blog.csdn.net/weixin_33676492/article/details/87477967