【问题标题】:Invoke a jar file in the M2 repository调用 M2 存储库中的 jar 文件
【发布时间】:2016-01-09 08:49:03
【问题描述】:

我有一个项目,我想在当前项目的后期执行阶段调用 M2 repo 中的另一个 Jar 文件。

我的 POM 的示例骨架

<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-plugin,但遇到以下问题;

  1. 我需要在哪里指定 JarToInvoke 依赖项?作为项目依赖还是作为 exec-plugin 依赖?

  2. 通过硬编码工作目录(/C:/repo 的路径),我可以调用 JarToInvoke 工件。但这不是一个好的解决方案,因为最后这个项目应该在任何具有不同操作系统的 m/c 中运行。那么如何让 exec-plugin 在项目的 M2 存储库中搜索 JarToInvoke 工件(默认类路径)?

3.在工作目录中对 M2 存储库路径进行硬编码时,我能够调用 JarToInvoke 工件。但是在运行 JarToInvoke 工件时,它会引发另一个依赖问题,无法找到 JarToInvoke 的一些 log4j 依赖项。我将 JarToInvoke 制作为带阴影的 jar,它按预期工作。但这不是一个永久或好的解决方案(因为阴影 jar 大小为 35 MB)。如何指示 exec-plugin 在 M2 repo 中查找依赖的 Jars。

请分享您的建议。提前致谢。

【问题讨论】:

    标签: maven


    【解决方案1】:

    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 依赖插件的copy-dependencies 目标将依赖项从本地存储库复制到预定义位置(例如${project.build.directory}/exec -jars),然后您可以在 exec 插件的 workingDirectory 配置选项中提供此目录。

    【讨论】:

    • 感谢 prunge 提供的宝贵信息。但是按照上面的方式配置POM后,抛出如下异常;
    • 'java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl。在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) 在 java.lang 调用(DelegatingMethodAccessorImpl.java:25)。 Thread.run(Thread.java:662) '
    • @RahulR.Prasad 在执行 JAR 时,它看起来像是 JarToInvoke 预期的不同版本的 asm 库。尝试为您的 com.tvworks.testing.tools 类所需的 asm 版本显式添加对 exec 插件的依赖项。
    • ---> 感谢 Prunge,我能够使用 Maven 依赖项的“复制依赖项”目标将依赖项复制到“${project.build.directory}/exec-jars)”插入。我还使用 maven exec 插件在 exec-jar 目录中调用了复制的 jar。谢谢你指导我。
    【解决方案2】:

    找到 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 2014-04-13
      • 2015-05-13
      • 2015-11-22
      • 2017-04-04
      • 2011-11-02
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多