2012-03-23 54 views
3

我有一個項目,我想在當前項目的執行階段調用M2倉庫中的另一個Jar文件。我POM調用M2倉庫中的jar文件

<plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>1.1</version> 

      <executions> 
       <execution> 
       <id>exec-one</id> 
       <phase>verify</phase> 
       <configuration> 
        executable>java</executable> 
        <arguments> <argument>-jar</argument> 
        <argument>JarToInvoke.jar</argument> 
        </arguments>     
        <**workingDirectory**>/C:/path to repo</workingDirectory> 
        </configuration> 
         <goals> 
         <goal>exec</goal> 
         </goals> 
        </execution> 
        </executions> 

       <dependencies> <dependency> 
       <groupId>GroupId of JarToInvoke</groupId> 
       <artifactId>JarToInvoke</artifactId> 
       <version>1.0.0-SNAPSHOT</version> 
       </dependency> 
       </dependencies> 
      </plugin>  
      </plugins> 

樣品骨架我試圖與Maven的EXEC-插件,但具有下列問題;

  1. 我在哪裏需要指定JarToInvoke依賴?作爲項目依賴項還是作爲exec-plugin依賴項?

  2. 通過硬編碼工作目錄(/ C:/ repo的路徑),我可以調用JarToInvoke工件。但這不是一個好的解決方案,因爲最後這個項目應該運行在任何具有不同操作系統的m/c上。那麼我怎樣才能讓exec-plugin在項目的M2倉庫(默認classpath)中搜索JarToInvoke工件呢?

3.在工作目錄中對M2 repo路徑進行硬編碼時,我能夠調用JarToInvoke工件。但是,在運行JarToInvoke工件時,會引發另一個依賴性問題,對JarToInvoke的某些log4j依賴關係找不到。我將JarToInvoke製作成陰影罐,並且按預期工作。但它不是一個永久的或很好的解決方案(因爲陰影的jar大小是35 MB)。我該如何指示exec-plugin在M2回購庫中尋找相關的Jars。

請分享您的建議。提前致謝。

回答

0

這個來自Exec插件文檔的example page描述了我想要的東西。

如果您可以使用exec:java目標而不是exec:exec,那麼爲您找到JVM是很重要的。您還可以通過更改插件的includeProjectDependenciesincludePluginDependencies配置選項來引入插件依賴項或項目依賴項。

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.1</version> 

    <executions> 
     <execution> 
      <id>exec-one</id> 
      <phase>verify</phase> 
      <configuration> 
       <includeProjectDependencies>false</includeProjectDependencies> 
       <includePluginDependencies>true</includePluginDependencies> 
       <executableDependency> 
        <groupId>GroupId of JarToInvoke</groupId> 
        <artifactId>JarToInvoke</artifactId> 
       </executableDependency> 

       <!-- Look up the main class from the manifest inside your dependency's JAR --> 
       <mainClass>com.example.Main</mainClass> 
       <arguments> 
        <!-- Add any arguments after your JAR here ---> 
       </arguments> 
      </configuration> 
      <goals> 
       <goal>java</goal> 
      </goals> 
     </execution> 
    </executions> 

    <dependencies> 
     <dependency> 
      <groupId>GroupId of JarToInvoke</groupId> 
      <artifactId>JarToInvoke</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
     </dependency> 
    </dependencies> 
</plugin>  

唯一的缺點是你必須顯式指定JAR中的主類來運行。您可以通過在依賴JAR中打開清單並閱讀Main-Class屬性來查看。

如果你真的需要使用exec:exec,你可以使用Maven Dependency插件的copy-dependencies目標從本地資源庫複製依賴到預定位置(如${project.build.directory}/EXEC-罐),然後就可以在這個目錄飼料在exec插件的workingDirectory配置選項中。

+0

感謝prunge的寶貴信息。但是,如上所述配置POM後,它會拋出以下異常; – appu 2012-03-23 10:51:05

+0

'java.lang.reflect.InvocationTargetException \t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) \t at sun.reflect.NativeMethodAccessorImpl。調用(NativeMethodAccessorImpl.java:39) \t在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) \t在java.lang.reflect.Method.invoke(Method.java:597) \t在org.codehaus .mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java:297)\t在java.lang.Thread.run(Thread.java:662)' – appu 2012-03-23 11:04:01

+0

@ RahulR.Prasad它看起來像一個不同版本的ASM庫的什麼JarToInvoke預期在執行JAR時被拉入。嘗試顯式地爲exec插件添加一個依賴項,以獲得您的'com.tvworks.testing.tools'類所需的asm版本。 – prunge 2012-03-23 16:59:10

0

定位到jar文件絕對路徑的一種更簡單的方法可能是使用maven-dependency-pluginproperties目標。

<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> 
<plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     <version>1.1</version> 

     <executions> 
      <execution> 
      <id>exec-one</id> 
      <phase>verify</phase> 
      <configuration> 
       <executable>java</executable> 
       <arguments> 
        <argument>-jar</argument> 
        <argument>${GroupIdofJarToInvoke:JarToInvoke:jar}</argument> 
       </arguments>     
       <workingDirectory>/C:/path to repo</workingDirectory> 
       </configuration> 
        <goals> 
         <goal>exec</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin>  
     </plugins> 

     <dependencies> 
      <dependency> 
       <groupId>GroupIdofJarToInvoke</groupId> 
       <artifactId>JarToInvoke</artifactId> 
       <version>1.0.0-SNAPSHOT</version> 
      </dependency> 
     <dependencies> 
相關問題