调用M2存储库中的jar文件 [英] Invoke a jar file in the M2 repository

查看:62
本文介绍了调用M2存储库中的jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project, in which I want to invoke another Jar file in M2 repo during the post execution phase of the current project.

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

I tried with maven-exec-plugin, but having the following issues;

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

  1. Where I need to specify to JarToInvoke dependency ? As a project dependency or as a exec-plugin dependency ?

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

With hard coding the working directory(/C:/path to repo), I am able to invoke the JarToInvoke artifact. But it is not a good solution, because finally this project should run in any m/c with different OS's. So how can I make the exec-plugin to search for the JarToInvoke artifact in the M2 repo of the project(default classpath) ?

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

3.While hard coding the M2 repo path in the working directory, I was able to invoke the JarToInvoke artifact. But while running the JarToInvoke artifact, it throws another dependency issue, some of the log4j dependencies to the JarToInvoke could not find. I made the JarToInvoke as a shaded jar and it work as expected. But it is not a permanent or good solution(Because the shaded jar size is of 35 MB). How can I instruct the exec-plugin to look for the dependent Jars in M2 repo.

请分享您的建议.预先感谢.

Please share your suggestions. Thanks in Advance.

推荐答案

如果可以使用exec:java目标而不是exec:exec目标,则查找JVM将为您解决.您还可以通过更改插件的includeProjectDependenciesincludePluginDependencies配置选项来获取插件依赖性或项目依赖性.

If you could use the exec:java goal instead of exec:exec, finding the JVM is taken care of for you. You can also pull in either plugin dependencies or project dependencies by changing the includeProjectDependencies and includePluginDependencies configuration options of the 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>
                <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属性.

The only disadvantage is that you have to explicitly specify the main class in the JAR to run. You can look this up by opening up the manifest in the dependency JAR and read the Main-Class attribute.

如果您真的需要使用exec:exec,则可以使用Maven依赖插件的

If you really need to use exec:exec, you could use the Maven Dependency Plugin's copy-dependencies goal to copy dependencies from your local repository to a predefined location (such as ${project.build.directory}/exec-jars) and then you can feed this directory in the exec plugin's workingDirectory configuration option.

这篇关于调用M2存储库中的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆