我可以使用Maven依赖项的path作为属性吗?
我在我的pom.xml中有一个maven依赖项,如下所示:
<dependency> <groupId>com.foo</groupId> <artifactId>Bar</artifactId> <version>1.2.3</version> </dependency>
我想使用二进制的系统path作为属性(所以我可以将它传递给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>
我有这样的可能吗?
假设com.foo:Bar:jar:1.2.3
工件在您的POM中声明为依赖,下面的属性返回本地资源库中jar的path:
${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进程资源 [INFO]扫描项目... [INFO] ----------------------------------------------- ------------------------- [INFO]build筑q2359872 [INFO] task-segment:[process-resources] [INFO] ----------------------------------------------- ------------------------- [INFO] [resources:resources {execution:default-resources}] [INFO]使用“UTF-8”编码来复制过滤的资源。 [INFO]跳过不存在的资源目录/ home / pascal / Projects / stackoverflow / q2359872 / src / main / resources [INFO] [antrun:run {execution:default}] [信息]执行任务 [echo] /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar [信息]执行的任务 [INFO] ----------------------------------------------- ------------------------- [信息]build立成功 [INFO] ----------------------------------------------- ------------------------- [信息]总时间:7秒 [INFO]完成date:二月二日星期二14:41:32 CET 2010 [INFO]最终内存:7M / 68M [INFO] ----------------------------------------------- -------------------------
下面是一个正确的实现,使用maven-dependency-plugin属性目标 ,它可以在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] ------------------------------------------------------------------------
有一个插件可能是你正在寻找… bitstrings.org(家庭) 。
如果没有上面的工作,你总是可以使用gmaven侵入MavenProject
对象,并获得你的神器的信息。 就我而言,我在configuration文件中声明了以下工件:
<!-- 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>
为了得到它的path,并把它放在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>
这是一种powershell的方法,但它的效果比我在那里看到的以前的解决scheme要好得多。
您需要编写一个新的Maven插件,将属性值设置为依赖关系的完全parsingpath名。 maven-dependency-plugin不会为你做。
它会将你的依赖关系复制到某个地方,然后你可以用这个path名来引用它。