【问题标题】:How can I automatically deploy external jars to my repository as part of my build process?作为构建过程的一部分,如何自动将外部 jar 部署到我的存储库?
【发布时间】:2011-06-14 05:08:40
【问题描述】:

我正在开发一个 Maven 项目,该项目建立在不使用 Maven 的第三方提供的库之上。他们每两周发布一次新版本。

我正在尝试尽可能多地自动化使代码在我们的项目中可用所涉及的工作。其中一项任务是从目录中获取一组 jar,并将它们作为工件上传到我们的存储库。

是否可以将此步骤作为构建的一部分?理想情况下,我希望最终得到一个看起来像这样的转换项目。

<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.convertor</groupId>
    <artifactId>thirdpartyconvertor</artifactId>
    <version>THIRD_PARTY_VERSION</version>
    <packaging>jar</packaging>

    <properties>
        <jarLocation>${someKnownLocation}\${version}</caplinSdkVersion>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!--
                    Mystery plugin that goes through the third party jar directory and deploys each jar file as
                    <groupId>com.thirdparty</groupId>
                    <artifactId>THE_JAR_NAME</artifactId>
                    <version>${version}</version>
                -->
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>com.thirdparty</groupId>
            <artifactId>all-jars</artifactId>
            <version>${version}</version>
        </dependency>

    </dependencies>
</project>

有什么想法吗?

【问题讨论】:

    标签: maven jar build


    【解决方案1】:

    有点味道,但我最终还是使用了 maven ant 插件来运行 maven ant 任务来完成工作。

    最终结果是特定目录中的所有 jar 文件都部署到工件并创建了一个依赖于所有添加的 jar 项目的进一步项目。

    <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <id>install</id>
                            <phase>install</phase>
                            <configuration>
                                <target>
                                    <taskdef resource="net/sf/antcontrib/antlib.xml">
                                        <classpath>
                                            <pathelement
                                                    location="${releaseDirectory}\thirdparty\common\antcontrib\1.0b3\ant-contrib-1.0b3.jar"/>
                                        </classpath>
                                    </taskdef>
    
                                    <taskdef resource="org/apache/maven/artifact/ant/antlib.xml">
                                        <classpath>
                                            <pathelement
                                                    location="${releaseDirectory}\thirdparty\common\maven-ant-tasks\2.1.1\maven-ant-tasks-2.1.1.jar"/>
                                        </classpath>
                                    </taskdef>
    
                                    <!-- write a pom that depends on all the jars we find. -->
                                    <var name="temp.pom.file" value="${build.directory}/maven/combined/pom.xml"/>
                                    <echo message='&lt;?xml version="1.0" encoding="UTF-8"?&gt;${line.separator}'
                                          file='${temp.pom.file}'/>
                                    <echo message='&lt;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"&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;groupId&gt;com.mavenised&lt;/groupId&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;artifactId&gt;combined-java&lt;/artifactId&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;version&gt;${version}&lt;/version&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;packaging&gt;pom&lt;/packaging&gt;${line.separator}'
                                          file='${temp.pom.file}' append='true'/>
                                    <echo message=' &lt;dependencies&gt;${line.separator}' file='${temp.pom.file}'
                                          append='true'/>
    
                                    <for param="file">
                                        <path>
                                            <fileset dir="${sdkDirectory}\lib\servlet">
                                                <include name="**/*.jar"/>
                                            </fileset>
                                        </path>
    
                                        <sequential>
                                            <propertyregex override="yes"
                                                           property="jarName"
                                                           input="@{file}"
                                                           regexp="([^/\\]+)\.jar"
                                                           select="\1"/>
    
                                            <pom id="jarPom" groupId="com.mavenised" artifactId="${jarName}"
                                                 version="${version}" name="${jarName}"/>
    
                                            <!-- the pom must be written to disk because of a bug in the ant plugin -->
                                            <writepom pomRefId="jarPom" file="${build.directory}/maven/pom.xml"/>
                                            <pom id="writtenPom" file="${build.directory}/maven/pom.xml"/>
    
                                            <install file="@{file}">
                                                <pom refid="writtenPom"/>
                                            </install>
    
                                            <echo message='  &lt;dependency&gt;${line.separator}' file='${temp.pom.file}' append='true'/>
                                            <echo message='   &lt;groupId&gt;com&lt;/groupId&gt;${line.separator}' file='${temp.pom.file}' append='true'/>
                                            <echo message='   &lt;artifactId&gt;${jarName}&lt;/artifactId&gt;${line.separator}' file='${temp.pom.file}' append='true'/>
                                            <echo message='   &lt;version&gt;${version}&lt;/version&gt;${line.separator}' file='${temp.pom.file}' append='true'/>
                                            <echo message='  &lt;/dependency&gt;${line.separator}' file='${temp.pom.file}' append='true'/>
    
                                        </sequential>
                                    </for>
    
                                    <echo message=' &lt;/dependencies&gt;${line.separator}' file='${temp.pom.file}'
                                          append='true'/>
                                    <echo message='&lt;/project&gt;${line.separator}' file='${temp.pom.file}'
                                          append='true'/>
    
                                    <pom id="combinedPom" file="${temp.pom.file}"/>
    
                                    <install file="${temp.pom.file}">
                                        <pom refid="combinedPom"/>
                                    </install>
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
    

    【讨论】:

      【解决方案2】:

      编辑:我知道问题是关于“自动”执行此操作的方法,但是我不知道有任何自动方法可以达到预期结果,所以我给出了一个稍微不太理想的手动替代方案达到同样的结果。

      有几种方法可以做到这一点。以下两种可能的解决方案都围绕在存储库中手动安装 jar 展开。我不知道有任何插件可以满足您的要求(但它不存在 - 还没有!) - 如果没有人可以推荐一个插件,您总是可以自己编写这样的插件! ;-)

      1) 第一种方法是每次手动将给定的 jar 安装到本地存储库中,每次插入时都会增加每个 jar 的版本号。

      mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
      

      然后您可以将 jar 称为另一个依赖项。但是,我认为您需要在每个版本中不断更改您的 pom 中的版本。 (我记得看到了一种始终引用最新版本的方法,但我认为它适用于 Maven v1,而且我还没有在 Maven 2 中使用它 - 我相信有人会添加评论指示如何引用最新版本(如果可能)

      2) 如果您的本地开发团队不止几个人,第二种方法会很有用——那就是有一个存储库管理器(Apache Archiva 只是我个人使用的一个例子——那里有很多!),并使用 Repo Manager UI 在存储库中安装每个 jar。这种方法的好处是团队只需要安装 Jar 的每个版本一次,而不是以前的方法,后者需要团队的每个成员在他们的本地存储库中安装每个版本的 jar。

      不知道有没有帮助!

      【讨论】:

      • 是的,我正在寻找更自动化的东西。
      【解决方案3】:

      您在问题中提到“自动”,我假设您有某种 CI 工具,例如 Jenkins。如果您使用的是 Jenkins,则可以使用 XShell 插件添加命令行作业。

      https://wiki.jenkins-ci.org/display/JENKINS/XShell+Plugin

      您可以编写一个批处理/脚本,从发布者那里下载库,然后将工件上传到存储库。

      您的批处理/脚本可以自动管理版本号等,Jenkins 可以自动处理定期更新。一旦你这样做了,你的项目也可以由 Jenkins 以你的新 XShell 作业作为父级来构建。

      或者,您可以使用 Maven Deploy 插件,而不是编写批处理/脚本文件:

      http://maven.apache.org/plugins/maven-deploy-plugin/

      要使用 maven-deploy 插件部署 3rd 方库,您仍然需要执行命令行,因此使用 Jenkins 或某种预定的命令行工具将使您“自动”。

      【讨论】:

      • 我们的团队城市正在运行。批处理文件可能会起到将罐子放入存储库的作用。还有很多其他的任务需要同时完成。 (根据 jars 中的类创建插件,提取一堆 javascript 并将其转换为 maven javascript 项目)。我最终得到了一个嵌套的 maven 项目,其中一个模块正在执行 jar 到 maven 项目的转换。
      猜你喜欢
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多