【问题标题】:Installing maven generated artifacts without using the artifactId在不使用 artifactId 的情况下安装 maven 生成的工件
【发布时间】:2020-02-22 21:17:21
【问题描述】:

我有一个包含 2 个模块的 Maven 父项目。在构建父项目时,我想生成并安装与 pom.xml 中使用的 artifactId 名称不同的最终工件。我可以看到生成的工件确实具有我想在构建目录中使用的名称。但是,当存档安装在本地 Maven 存储库中时,maven-install-plugin 使用项目的 artifactId 复制工件。如何在不使用 artifactId 的情况下配置插件来安装工件?

我的子项目的 pom.xml:

<project>
   <parent>
      <groupId>com.mycomp</groupId>
      <artifactId>com-mycomp</artifactId>   
      <version>12.0.0.0.0</version>
   </parent>

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.mycomp</groupId>
   <artifactId>child1</artifactId>
   <version>12.0.0.0.0</version>

   <build>
      <defaultGoal>install</defaultGoal>
      <finalName>${parent.artifactId}-${project.version}</finalName>
      <plugins>
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>runtime</includeScope>
                       <outputDirectory>${project.build.directory}/dependencies/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>assembly:package</id>
                        <phase>package</phase>
                        <goals>
                                <goal>single</goal>
                        </goals>
                        <configuration>
                                <appendAssemblyId>false</appendAssemblyId>
                                <descriptors>
                  <descriptor>src/main/resources/dependencies.xml</descriptor>
                                </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

                        <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-jar-plugin</artifactId>
                           <version>3.1.2</version>
                           <configuration>
                               <archive>
                                   <manifest>
                                       <classpathPrefix>libs/</classpathPrefix>
                                       <addClasspath>true</addClasspath>
                                       <mainClass>com.my.MainClass</mainClass>
                                   </manifest>
                               </archive>
                           </configuration>
                       </plugin>

      </plugins>
   </build>
</project>

父pom.xml:

<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.mycomp</groupId>
   <artifactId>com-mycomp</artifactId>   
   <version>12.0.0.0.0</version>
   <packaging>pom</packaging>

   <modules>
     <module>child1</module>
     <module>child2</module>
   </modules>
   :
   :
</project>

mvn clean install 的期望输出: 1)本地maven仓库中的com-mycomp-12.0.0.0.0.jar 2) com-mycomp-12.0.0.0.0.zip 包含上述 jar 及其依赖项。

观察到的行为: 3) com-mycomp-12.0.0.0.0.jar 在构建目录中创建。但它作为 child1-12.0.0.0.0.jar 安装在 maven 存储库中 4) com-mycomp-12.0.0.0.0.zip 在构建目录中创建。但它作为 child1-12.0.0.0.0.zip 安装在 maven 存储库中

如何实现所需的行为(#1 和 #2)?

【问题讨论】:

    标签: maven


    【解决方案1】:

    本地存储库中的工件始终与其 artifactId 一起安装。

    你无法改变它。即使可以,我也强烈建议您不要这样做,因为您超出了预期。

    如果您想创建一个包含依赖项的 jar 的附加 zip,则不需要第二个模块。您可以通过程序集插件创建它,并将其与dependencies 之类的分类器一起附加。然后你有两个工件像

    com-mycomp-12.0.0.0.0.jar
    com-mycomp-12.0.0.0.0-dependencies.zip
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 2015-10-20
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多