2010-03-01 87 views
36

我在我的pom.xml Maven的依賴這樣:我可以使用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> 

我類似的東西可能嗎?

+0

我有點困惑:如果你想引用'Bar.jar'作爲一個系統庫,你需要指定'系統 $ {} my.lib'但似乎你想使用'$ {my.lib}'在別的地方。顯示如何使用'$ {my.lib}'的完整示例... – 2010-03-01 23:40:29

+1

@dma_k OP想要將物理路徑傳遞給由maven觸發的外部進程的依賴項。 – 2010-03-01 23:44:08

回答

36

假設com.foo:Bar:jar:1.2.3神器被聲明爲您的POM的依賴,下面的屬性返回本地倉庫的路徑罐子:

${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] ------------------------------------------------------------------------ 
+1

我無法證明此功能在Maven中有效。這隻適用於'maven-antrun-plugin'(參見http://jira.codehaus.org/browse/MANTRUN-110)。請提供一個完整的pom示例,正如我所想的那樣,您不是指'',而是其他一些屬性。 – 2010-03-02 13:18:48

+0

@dma_k您提到的Jira問題不會顯示任何內容,只是antrun文檔中存在錯誤。現在,請隨時自行測試此解決方案。順便說一句,我總是測試我的答案:) – 2010-03-02 13:53:34

+0

@Pascal感謝您的更新!我完全相信你,它可以在你的網站上運行:)我的問題是:是否應該與'maven-antrun-plugin'結合使用。你用你的例子展示這個,太好了!從例子中我看到這是'maven-antrun-plugin'特有的功能,也就是說,如果我想用'$ {my.lib}'變量替換資源(不使用任何額外的pugin) - 我無法做到這一點,對? – 2010-03-02 16:05:07

0

您需要編寫一個新的maven插件,將屬性值設置爲依賴項的完全解析路徑名。 maven-dependency-plugin不會爲你做到這一點。

它會複製你的依賴某處然後你可以通過該路徑名引用它。

47

這是一個正確的實現,使用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> 

,輸出爲...

[email protected] /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] ------------------------------------------------------------------------ 
+2

這對我來說非常合適。感謝帖子! – kurzweil4 2014-09-15 06:47:14

+0

如果您的示例使用了不同的依賴關係,則可能不那麼令人困惑。 對於那些有興趣,如果你需要JMockit罐子,你的財產會是這樣: $ {org.jmockit:jmockit:罐子} 也就是說: $ {的groupId:artifactId的:罐} – kurzweil4 2014-09-15 06:56:00

+0

好點,編輯。 – 2014-09-17 16:12:38

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> 

這是某種的蠻力方法,但它的工作原理遠遠好於我以前見過的解決方案。

相關問題