代码之家  ›  专栏  ›  技术社区  ›  Mauli

是否可以明确地告诉Maven下载工件并将其安装到本地存储库?

  •  2
  • Mauli  · 技术社区  · 16 年前

    是的,我读 Utility for downloading artifacts from maven repo without mvn/poms 以及其他相关问题,但我不想手动安装该文件。事实上,我想要像 WGET 对于maven,它获取一个工件(带有依赖项),并将其放在某个地方或安装在本地存储库中。可能有一个插件可用吗?

    5 回复  |  直到 14 年前
        1
  •  2
  •   tuler    16 年前

    dependency:get 目标,如 dependency plugin manual .

    此功能是在此请求的 jira ticket .

    但我用Maven2.1.0试过,但没用。

        2
  •  2
  •   laz    14 年前

    我不确定是否有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.

        3
  •  0
  •   jitter    16 年前

    你的意思是一种不向 XML ?

    因为通过向您的 XML Maven依赖项管理自动从指定的存储库中检索工件,并将其安装到本地存储库。

    <dependencies>
      <dependency>
        <groupId>foo</groupId>
        <artifactId>foo</artifactId>
        <version>1.0.1</version>
      </dependency>
    </dependencies>
    
        4
  •  0
  •   Scott Bale    16 年前
        5
  •  0
  •   lumpynose    16 年前

    我使用下面的shell脚本安装一个commons日志替换,但在这种情况下,我已经准备好了JAR:

    #! /bin/sh
    
    mvn install:install-file \
        -DgroupId=commons-logging \
        -DartifactId=commons-logging \
        -Dversion=99.0-does-not-exist \
        -Dpackaging=jar \
        -DcreateChecksum=true \
        -DpomFile=commons-logging-99.0-does-not-exist.pom \
        -Dfile=commons-logging-99.0-does-not-exist.jar
    
    mvn install:install-file \
        -DgroupId=commons-logging \
        -DartifactId=commons-logging-api \
        -Dversion=99.0-does-not-exist \
        -Dpackaging=jar \
        -DcreateChecksum=true \
        -DpomFile=commons-logging-api-99.0-does-not-exist.pom \
        -Dfile=commons-logging-api-99.0-does-not-exist.jar
    

    当你说“本地存储库”时,你的意思是你自己的,作为一个个体,比如说Unix上~/.m2中的那个,还是你的公司存储库?这是一个不同的问题。对于前者,我有点紧张;只需将条目添加到您的pom中即可。对于后者,请检查存储库服务器的文档以了解如何获取Central的副本。

    推荐文章