【问题标题】:Is it possible to explicity tell maven to download and install an artifact to the local repository?是否可以明确告诉 maven 下载并将工件安装到本地存储库?
【发布时间】:2009-06-18 16:39:26
【问题描述】:

是的,我阅读了Utility for downloading artifacts from maven repo without mvn/poms 和其他相关问题,但我不想手动安装文件。实际上,我想为 maven 提供类似 wget 的东西,它获取一个工件(带有依赖项)并将其放在某个地方或安装在本地存储库中。是否有可用的插件可以做到这一点?

【问题讨论】:

    标签: java maven-2 build


    【解决方案1】:

    dependency:get 目标,如dependency plugin manual 所述。

    此专题是通过jira ticket 请求的。

    但我在 Maven 2.1.0 上尝试过,但没有成功。

    【讨论】:

    • 这就是我要找的。​​span>
    • 完整的命令是mvn dependency:get -Dartifact=<group>:<artifact-id>:<version>
    【解决方案2】:

    我不确定是否有 Maven 插件来处理这个问题,但使用 Maven Ant tasks 来实现这个目的相当简单。你不需要 Maven 或 POM 文件,只需要 Ant 和这个构建文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="mvn-get" default="get" basedir=".">
        <property name="maven.ant.tasks.jar" value="${ant.home}/lib/maven-ant-tasks-2.011.jar" />
        <property name="maven.ant.tasks.bootstrap.location" value="http://apache.inetbridge.net/maven/binaries/maven-ant-tasks-2.0.11.jar" />
        <available property="maven.ant.tasks.jar.exists" file="${maven.ant.tasks.jar}" />
    
        <!-- This will download the "latest version" of the maven-ant-tasks if needed -->
        <target name="bootstrap_maven" unless="maven.ant.tasks.jar.exists">
            <get src="${maven.ant.tasks.bootstrap.location}" dest="${maven.ant.tasks.jar}" />
        </target>
    
        <!-- This will initialize all the maven ant tasks and download the requested artifact and its dependencies -->
        <target name="get" depends="bootstrap_maven" xmlns:artifact="urn:maven-artifact-ant">
            <path id="maven.ant.tasks.classpath" path="${maven.ant.tasks.jar}" />
            <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" classpathref="maven.ant.tasks.classpath" />
            <condition property="maven.repo.local" value="${maven.repo.local}" else="${user.home}/.m2/repository">
                <isset property="maven.repo.local" />
            </condition>
            <echo>maven.repo.local=${maven.repo.local}</echo>
            <artifact:localRepository id="local.repository" path="${maven.repo.local}" />
            <artifact:dependencies pathId="build.classpath" sourcesFilesetId="sources.id">
                <dependency groupId="${mvn.get.groupId}" artifactId="${mvn.get.artifactId}" version="${mvn.get.version}"/>
                <localRepository refid="local.repository" />
            </artifact:dependencies>
        </target>
    
    </project>
    

    使用如下命令行可以满足您的需求:

    ant -Dmvn.get.groupId=commons-httpclient -Dmvn.get.artifactId=commons-httpclient -Dmvn.get.version=3.1 -Dmaven.repo.local=.
    

    这个灵感来自excellent blog post.

    【讨论】:

    • 这是个好主意,但我一直在寻找 tuler 的回答。
    【解决方案3】:

    你的意思是一种不向pom.xml添加依赖的方式?

    因为通过向您的 pom.xml 添加依赖项,maven 依赖项管理会自动从您指定的存储库中检索工件并将其安装到本地存储库。

    <dependencies>
      <dependency>
        <groupId>foo</groupId>
        <artifactId>foo</artifactId>
        <version>1.0.1</version>
      </dependency>
    </dependencies>
    

    【讨论】:

      【解决方案4】:

      【讨论】:

        【解决方案5】:

        我使用以下 shell 脚本来安装 Commons Logging 替代品,但在这种情况下,我已经有了 jars:

        #! /bin/sh mvn install:安装文件\ -DgroupId=commons-日志记录\ -DartifactId=commons-日志记录\ -Dversion=99.0-不存在\ -D包装=罐子\ -DcreateChecksum=真\ -DpomFile=commons-logging-99.0-does-not-exist.pom \ -Dfile=commons-logging-99.0-does-not-exist.jar mvn install:安装文件\ -DgroupId=commons-日志记录\ -DartifactId=commons-logging-api\ -Dversion=99.0-不存在\ -D包装=罐子\ -DcreateChecksum=真\ -DpomFile=commons-logging-api-99.0-does-not-exist.pom \ -Dfile=commons-logging-api-99.0-does-not-exist.jar

        当您说“本地存储库”时,您是指您自己的个人存储库,即 unix 上 ~/.m2 中的存储库,还是您的公司存储库?这是一个不同的问题。对于前者,我有抖动;只需将条目添加到您的 pom.xml 中即可。对于后者,请查看存储库服务器的文档以了解如何获取 Central 的副本。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-26
          • 2012-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-07
          • 1970-01-01
          相关资源
          最近更新 更多