如何在构建过程中自动将外部jar部署到存储库中



我正在开发一个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 ant插件来运行maven ant任务来完成任务。

最终的结果是,特定目录中的所有jar文件都部署到artifactory,并创建了一个依赖于所有添加的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}thirdpartycommonantcontrib1.0b3ant-contrib-1.0b3.jar"/>
                                    </classpath>
                                </taskdef>
                                <taskdef resource="org/apache/maven/artifact/ant/antlib.xml">
                                    <classpath>
                                        <pathelement
                                                location="${releaseDirectory}thirdpartycommonmaven-ant-tasks2.1.1maven-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}libservlet">
                                            <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>

EDIT:我知道问题是关于"自动"实现这一点的方法,但我不知道有任何自动实现所需结果的方法,所以我给出了一个稍微不太优化的手动实现相同结果的替代方案

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

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

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

然后,您可以将jar称为另一个依赖项。然而,我认为您需要在每次发布时不断更改pom中的版本。

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

我不知道这有没有帮助!

你在问题中提到"自动",我认为你有像Jenkins这样的CI工具。如果您正在使用Jenkins,您可以使用XShell插件添加命令行作业。

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

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

您的批处理/脚本可以自动管理版本号等,Jenkins可以自动处理定期更新。一旦你做到了这一点,你的项目也可以由Jenkins用你的新XShell工作作为家长来构建。

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

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

要使用maven部署插件部署第三方库,您仍然需要执行命令行,因此使用Jenkins或某种预定的命令行工具将使您进入"自动"状态

最新更新