开发者

How can I automatically deploy external jars to my repository as part of my build process?

I'm working on a Maven project that is built on top of libraries provided by a third party that does not use Maven. They provide new releases every two weeks.

I'm trying to automate as much as possible the work involved in getting the code usable in our projects. One of the tasks for this is taking a set of jars from a directory and uploading them to our repository as artifacts.

Is it possible to do this step as part of a build? Ideally I'd like to end up with a conversion project that looks something like.

<?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 par开发者_运维知识库ty 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>

Any ideas?


It smells a bit, but I've ended up using the maven ant plugin to run the maven ant task to get the job done.

The end result is that all the jar files in a particular directory are deployed to artifactory and a further project is created that depends on all the added jar projects.

<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>


EDIT: I know the question was regarding ways to do this "automatically," however I'm not aware of any automatic ways to achieve the desired results, so I'm giving a slightly less optimal alternative of manually achieving the same result.

There are a few ways to do this. The following 2 possible solutions all revolve around manually installing the jar in the repository. I'm not aware of any plugin that can do what you're asking (but that doesn't it doesn't exist - yet!) - you could always write such a plugin yourself if nobody can suggest one! ;-)

1) The first would be to manually install the given jar in your local repository manually each time, incrementing the version number of each jar each time you insert it.

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

You could then refer to the jar as just another dependency. However, I think you would need to keep changing the version in your pom with each release. (I remember seeing a way to always reference the latest version, but I think it was for Maven v1, and I've not had it working in Maven 2 - I'm sure someone will add a comment indicating how to reference the latest version, if it's possible)

2) The second way would be useful if you have a local development team of more than just a few people - that would be to have a repository manager (Apache Archiva is just one example that I've personally used - there are many out there!), and use the Repo Manager UI to install each jar in the repository. The benefit of this method is that the team would only need to install each version of the Jar once, rather than the previous method, which would require that each member of the team install each version of the jar in their local repository.

I don't know if thats been any help!


You mention "automatically" in your question and I would assume that you have some kind of CI tool like Jenkins. If you are using Jenkins, you can add command-line jobs using the XShell plugin.

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

You could write a batch/script that downloads the libraries from the publisher and then upload the artifact to the repository.

Your batch/script could automatically manage the version number, etc and Jenkins could handle the periodic update automatically. Once you do this, your project could also be built by Jenkins with your new XShell job as a parent.

Or, instead of writing a batch/script file, you might make use of the Maven Deploy plugin:

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

To deploy 3rd party libraries with the maven-deploy plugin, you still need to execute a command-line, so using Jenkins or some kind of scheduled command-line tool will get you to "automatic."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜