我可以使用Maven依赖项的路径作为属性吗?

时间:2010-03-01 23:25:12

标签: maven-2

我的pom.xml中有maven依赖关系:

<dependency>
    <groupId>com.foo</groupId>
    <artifactId>Bar</artifactId>
    <version>1.2.3</version>
</dependency>

我想使用二进制的系统路径作为属性(因此我可以将它传递给由maven启动的外部进程)。我可以用尴尬的方式做到这一点:

<properties>
    <my.lib>${settings.localRepository}/com/foo/Bar/1.2.3/Bar.jar</my.lib>
</properties>

但我真的想使用更标准的机制,例如:

<properties>
    <my.lib>${com.foo:Bar:1.2.3}</my.lib>
</properties>

我有可能这样吗?

5 个答案:

答案 0 :(得分:54)

这是一个正确的实现,使用maven-dependency-plugin properties goal,可以在pom中的任何位置使用:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stackoverflow</groupId>
    <artifactId>q2359872</artifactId>
    <version>2.0-SNAPSHOT</version>
    <name>q2359872</name>

    <properties>
        <!-- Must be listed in the dependencies section otherwise it will be null. -->
        <my.lib>${org.jmockit:jmockit:jar}</my.lib>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.11</version>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>generate-sources</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>properties</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Example usage: -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <configuration>
                    <executable>echo</executable>
                    <arguments>
                        <argument>path to jar=</argument>
                        <argument>${org.jmockit:jmockit:jar}</argument>
                        <argument>my.lib=</argument>
                        <argument>${my.lib}</argument>
                    </arguments>
                </configuration>
            </plugin>
            <!-- end of Example usage -->
        </plugins>
    </build>
</project>

输出是......

jpyeron@black /projects/wkspc/tmp/foo
$ /cygdrive/c/programs.x86_64/apache-software-foundation/apache-maven-3.1.1/bin/mvn
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building q2359872 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.3:properties (default) @ q2359872 ---
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) @ q2359872 ---
path to jar= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar my.lib= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.032s
[INFO] Finished at: Wed Sep 17 12:07:18 EDT 2014
[INFO] Final Memory: 10M/153M
[INFO] ------------------------------------------------------------------------

答案 1 :(得分:36)

假设在您的POM中将com.foo:Bar:jar:1.2.3工件声明为依赖项,则以下属性返回本地存储库中jar的路径:

${maven.dependency.com.foo.Bar.jar.path}

更新:这是一个简单的POM,证明了这一点:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>q2359872</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>q2359872</name>
  <properties>
    <my.lib>${maven.dependency.junit.junit.jar.path}</my.lib>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>process-resources</phase>
            <configuration>
              <tasks>
                <echo>${my.lib}</echo>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

运行mvn process-resources会产生以下输出:

$ mvn process-resources
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building q2359872
[INFO]    task-segment: [process-resources]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/q2359872/src/main/resources
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Tue Mar 02 14:41:32 CET 2010
[INFO] Final Memory: 7M/68M
[INFO] ------------------------------------------------------------------------

答案 2 :(得分:9)

有一个插件可能正是您要找的...... bitstrings.org (home)

答案 3 :(得分:1)

如果没有上层工作,你可以随时使用gmaven积极地潜入MavenProject对象并获取你的工件信息。在我的例子中,我在配置文件中声明了以下工件:

            <!-- Neo4J connector. This dependency is scoped to be usable by maven-exec-plugin 
                which installs it in Glassfish -->
            <dependency>
                <groupId>com.netoprise</groupId>
                <artifactId>neo4j-connector</artifactId>
                <version>${neo4j.connector.version}</version>
                <type>rar</type>
                <!-- Set in test scope to avoid release issues -->
                <scope>test</scope>
            </dependency>

为了获得它的路径并将其放在maven属性中,我编写了以下gmaven脚本:

                <!-- Small script used to build maven property for neo4j-connector path -->
                <plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <version>1.3</version>
                    <executions>
                        <execution>
                            <id>get-neo4j-connector-rar-path</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <source>
                                    <![CDATA[
println "initial value of neo4j.connector.rarPath is \""+project.properties['neo4j.connector.rarPath']+"\""                             

// Duplicate model in a Mavenproject, allowing me to get associated artifact
// So sad I can't get the embdder object

// More info here : http://maven.apache.org/ref/3.0.3/maven-core/apidocs/org/apache/maven/project/MavenProject.html
def mavenProject = new org.apache.maven.project.MavenProject(project)

// More infos on Artifact there : http://maven.apache.org/ref/3.0.3/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html
def neo4jConnector = mavenProject.getArtifacts().find { artifact -> artifact.getArtifactId()=='neo4j-connector' }
// Now resolve dependency to produce an artifact
// notice maven property interpolation doesn't do toString, so we have to do it ourselves
project.properties['neo4j.connector.rarPath'] = neo4jConnector.getFile().getAbsolutePath()

println "usable neoj4Connector can be found at "+project.properties['neo4j.connector.rarPath']

                                    ]]>
                                </source>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

这是一种蛮力方法,但它的效果远远超过我之前见过的解决方案。

答案 4 :(得分:0)

您需要编写一个新的maven插件,将属性值设置为依赖项的完全解析的路径名。 maven-dependency-plugin不会为你做那件事。

它会复制您的依赖关系,然后您可以通过该路径名来引用它。