java 为 exec-maven-plugin 添加额外的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2587935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:55:19  来源:igfitidea点击:

Add additional path to exec-maven-plugin

javamaven-2maven-plugin

提问by Kurt Pattyn

I would like to add an additional class path to the exec-maven-plugin.
Besides the %classpath, I would like to add an extra path to a directory containing resources (/Users/kornp/resources). Currently, my pom looks like this:

我想向 exec-maven-plugin 添加一个额外的类路径。
除了 %classpath,我想向包含资源的目录 (/Users/kornp/resources) 添加一个额外的路径。目前,我的 pom 看起来像这样:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1.1</version>
  <configuration>
    <executable>java</executable>
    <classpathScope>runtime</classpathScope>
    <arguments>
      <argument>%classpath:/Users/kornp/resources</argument>
      <argument>org.drrabbit.maventest.App</argument>
    </arguments>
  </configuration>
</plugin>

How should I configure this?

我应该如何配置它?

回答by F?rat Kü?üK

I have some configuration files in a specific directory outside of my source folder. So i defined additonal resources to my pom.xml file.

我的源文件夹外的特定目录中有一些配置文件。所以我为我的 pom.xml 文件定义了附加资源。

my sample directory structure is:

我的示例目录结构是:

+ src
+ conf
  - app.properties
  - log4j.xml
- pom.xml

my pom.xml:

我的 pom.xml:

<build>
  <resources>
    <resource>
      <directory>conf</directory>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>

  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <configuration>
        <executable>java</executable>
        <mainClass>com.mycompany.MyMainClass</mainClass>
      </configuration>
    </plugin>
  </plugins>
<build>

Now we may execute the program:

现在我们可以执行程序:

mvn clean compile exec:java

回答by Pascal Thivent

Did you try using the commandlineArgsparameter (as mentioned in the exec example)?

您是否尝试使用commandlineArgs参数(如exec 示例中所述)?

回答by Adrian Shum

Though it looks less elegant, but switching to antrun plugin should works:

虽然它看起来不太优雅,但切换到 antrun 插件应该可以:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>runSomething</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <property name="runtime_classpath" refid="maven.runtime.classpath"/>

                    <java classname="org.drrabbit.maventest.App" 
                            fork="true" 
                            failonerror="true" 
                            maxmemory="512m">

                        <classpath>
                            <pathelement path="${project.build.directory}/some/extra/resources" />
                            <pathelement path="${runtime_classpath}" />
                        </classpath>
                    </java>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

However, it doesn't seems a good idea to have the extra resources put in somewhere outside the project, as your op suggested. You should either consider putting that as part of the project, or make it a jar and deploy to maven repo so you can put it as a plugin dependency.

但是,正如您的操作所建议的那样,将额外资源放在项目之外的某个地方似乎不是一个好主意。您应该考虑将其作为项目的一部分,或者将其制作为 jar 并部署到 Maven 存储库,以便您可以将其作为插件依赖项。

回答by Kurt Pattyn

Okay,

好的,

I adapted the plugin so that the command line arguments can be fully specified (including the %classpath parameter)

我修改了插件,以便可以完全指定命令行参数(包括 %classpath 参数)