美文网首页
自定义maven archetype,上传到nexus,并使用n

自定义maven archetype,上传到nexus,并使用n

作者: 王大豆 | 来源:发表于2020-08-12 17:02 被阅读0次

    需要自定义骨架的需求

    有的时候,我们项目中会有很多公共的依赖、公共的代码、公共的配置文件
    但是我们又不希望创建一个新项目之后重新从老项目拷贝。所以我们能使用老的项目作为新建项目的maven archetype骨架。

    如何自定义骨架

    首先创建一个maven项目,这个项目是作为archetype骨架的项目


    创建骨架项目.png

    要想生成骨架,我们的maven要加一个插件pom.xml的build节点下加入以下代码

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

    构建archetype

    在项目pom.xml同级的目录下运行以下命令

    mvn clean archetype:create-from-project
    

    注意:一定要clean,不然使用骨架会把骨架的java项目结构也加到新项目中

    看到build success后我们会发现生成了target文件夹,结构如下:

    构建项目骨架1.png

    target目录下会有generated-sources目录,generated-sources/archetype/src/main.resource/META_INF.maven下会有一个archetype-metadata.xml文件,这里是可以配置那些资源会被包含在骨架中,那些不会包含在骨架中。

    安装骨架到本地仓库

    到generated-sources/archetype 目录下安装骨架到本地

    cd target/generated-sources/archetype/
    mvn clean install
    

    构建成功后会输出一下信息:

    [INFO] Installing E:\SpringIO\achetype-test\target\generated-sources\archetype\pom.xml to E:\MavenRepository\org\example\achetype-test-archetype\1.0-SNAPSHOT\achetype-test-archetype-1.0-SNAPSHOT.pom
    

    在本地按照路径找到achetype-test-archetype-1.0-SNAPSHOT.pom 打开,看看里面的信息:

    <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>org.example</groupId>
      <artifactId>achetype-test-archetype</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>maven-archetype</packaging>
    
      <name>achetype-test-archetype</name>
    
      <build>
        <extensions>
          <extension>
            <groupId>org.apache.maven.archetype</groupId>
            <artifactId>archetype-packaging</artifactId>
            <version>2.2</version>
          </extension>
        </extensions>
    
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-archetype-plugin</artifactId>
              <version>2.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    

    里面标识了jar包的类型是maven-archetype

    在本地仓库生成坐标信息

    执行一下命令:

    mvn archetype:crawl
    

    在指定的Maven库中查找可以的模板,并更新模板目录,这个时候在本地的maven仓库中就会生成一个archetype-catalog.xml文件,里面有固件的坐标信息。
    打开:

    <?xml version="1.0" encoding="UTF-8"?>
    <archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
        xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <archetypes>
        <archetype>
          <groupId>org.example</groupId>
          <artifactId>achetype-test-archetype</artifactId>
          <version>1.0-SNAPSHOT</version>
          <description>achetype-test</description>
        </archetype>
      </archetypes>
    </archetype-catalog>
    

    根据模板坐标信息创建新项目

    首先创建maven项目


    通过骨架创建新项目1.png

    然后把骨架的坐标信息输入


    通过骨架创建新项目2.png

    输入的坐标信息是archetype-catalog.xml里achetype-test-archetype骨架的坐标信息,输入完确认之后下面的archetype面板会出现以下骨架:这个信息会缓存在:
    C:\Users/[用户]\AppData\Local\JetBrains\IntelliJIdea2020.x.x\Maven\Indices/UserArchetypes.xml下
    如果想要清楚,打开,把你添加的骨架坐标信息删掉再清楚IDEA缓存重启就好


    通过骨架创建新项目3.png

    选择然后下一步,输入完项目的groupId和artifactId之后项目就创建成功了
    创建的项目会有骨架项目的java代码、依赖配置、以及资源文件


    通过骨架创建新项目4.png

    上传骨架到nexus

    在项目target/generated-sources/archetype/pom.xml 中加入以下配置,指定nexus地址。

    <distributionManagement>
            <repository>
                <id>nexus-releases</id>
                <name>Micaicms Releases</name>
                <url>http://localhost:8081/nexus/content/repositories/releases/</url>
            </repository>
            <snapshotRepository>
                <id>nexus-snapshots</id>
                <name>Micaicms Releases</name>
                <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
            </snapshotRepository>
    </distributionManagement>
    

    然后在target/generated-sources/archetype运行

    mvn deploy
    

    这样就能把骨架上传到nexus仓库中了(这个nexus只是笔者自己搭建的本地nexus服务)
    注意:上传nexus私服需要密码。

    使用远程nexus仓库的archetype创建项目

    使用远程nexus仓库的archetype创建项目的时候,必须在自己的maven conf 下settings.xml加入以下配置:

    因为Maven 3改变了原型存储库的集成方式。-DarchetypeRepository参数不再存在。相反,需要将archteype存储库添加到settings.xml

    <profile>
          <!-- the profile id is arbitrary 这个id是任意填的--> 
          <id>my_archetype</id>
          <repositories>
            <repository>
              <!-- the repository id has to be named: archetype 这repository Id必须是archetype -->
              <id>archetype</id>
              <name>my archetypes</name>
              <url>http://127.0.0.1:8081/repository/maven-public/</url>
              <releases>
                <enabled>true</enabled>
                <checksumPolicy>fail</checksumPolicy>
              </releases>
              <snapshots>
                <enabled>true</enabled>
                <checksumPolicy>warn</checksumPolicy>
              </snapshots>
            </repository>
          </repositories>
        </profile>
    
      <activeProfiles>
        <activeProfile>my_archetype</activeProfile> <!-- 这个id是上面的profile id -->
      </activeProfiles>
    

    注意:以上配置一定要加!以上配置一定要加!!以上配置一定要加!!!上面的配置是为了告诉maven archetype可以从哪里拿,如果没有上面的配置,使用远程nexus 的archetype的时候会报The desired archetype does not exist

    相关文章

      网友评论

          本文标题:自定义maven archetype,上传到nexus,并使用n

          本文链接:https://www.haomeiwen.com/subject/dqrmdktx.html