2012-03-23 57 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> 
相关问题