Java Maven Exec 插件:如何配置工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25431794/
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-08-11 00:22:43  来源:igfitidea点击:

Maven Exec Plugin : How configure the working directory

javamavenexec

提问by user1842947

I'm using the Exec Maven plugin with the following command :

我正在使用带有以下命令的 Exec Maven 插件:

mvn exec:java

mvn exec:java

and I didn't manage to set the working directorywith this execution mode. I want to use a mainClass (in a specific package) and I want the root folder of my execution in another directory than the ${basedir}.

我没有设法用这种执行模式设置工作目录。我想使用一个 mainClass(在一个特定的包中),并且我想要在 ${basedir} 之外的另一个目录中执行我的根文件夹。

Thank you for your help.

感谢您的帮助。

My pom.xml where the target < workingDirectory >doesn't work for me :

我的 pom.xml 目标<workingDirectory>对我不起作用:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
            <workingDirectory>${project.build.directory}\classes</workingDirectory>
            <mainClass>com.package.MyMainClass</mainClass>
            <includeProjectDependencies>true</includeProjectDependencies>
        </configuration>
  </plugin>

the result with the -X option

带有 -X 选项的结果

[DEBUG] Configuring mojo org.codehaus.mojo:exec-maven-plugin:1.3.2:java from plugin realm ClassRealm[plugin>org.codehaus.mojo:exec-maven-plugin:1.3.2,parent: sun.misc.Launcher$AppClassLoader@11b86e7]
[DEBUG] Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.3.2:java' with basic configurator -->
[DEBUG]   (f) arguments = []
[DEBUG]   (f) classpathScope = runtime
[DEBUG]   (f) cleanupDaemonThreads = true
[DEBUG]   (f) daemonThreadJoinTimeout = 15000
[DEBUG]   (f) includePluginDependencies = false
[DEBUG]   (f) includeProjectDependencies = true
[DEBUG]   (f) keepAlive = false
[DEBUG]   (f) killAfter = 1
[DEBUG]   (f) localRepository =        id: local url: file:///C:/Users/100728452/.m2/repository/   layout: none
[DEBUG]   (f) mainClass = com.package.MyMainClass
[DEBUG]   (f) pluginDependencies = [org.codehaus.mojo:exec-maven-plugin:maven-plugin:1.3.2:, org.codehaus.plexus:plexus...
[DEBUG]   (f) skip = false
[DEBUG]   (f) stopUnresponsiveDaemonThreads = false
[DEBUG]   (s) key = sun.java2d.ddoffscreen
[DEBUG]   (s) value = false
[DEBUG]   (s) key = com.odi.OStoreLicenseFile
[DEBUG]   (s) value = .\library\odi\etc\license.txt
[DEBUG]   (f) systemProperties = [org.codehaus.mojo.exec.Property@194e776, org.codehaus.mojo.exec.Property@e80740]
[DEBUG] -- end configuration --
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
[DEBUG] Invoking : com.mypackage.MyMainClass.main()
[DEBUG] Plugin Dependencies will be excluded.
[DEBUG] Project Dependencies will be included.
[DEBUG] Collected project artifacts [javax.help:javahelp:jar:2.0.02:compile,

回答by khmarbaise

If you like to set it via calling mvn exec:java ...you need to go via the property like this:

如果你想通过调用来设置它,mvn exec:java ...你需要通过这样的属性:

mvn exec:java -Dexec.workingdir=Folder ...

which has nothing to do what you defined in your pom cause calling the goal exec:javais not part of the life cycle.

这与您在 pom 中定义的内容无关,因为调用目标exec:java不是生命周期的一部分。

回答by user1842947

I didn't found solution with exec:java.

我没有找到 exec:java 的解决方案。

So, now I use exec:execinstead because we can set the workingDirectory and it' OK.

所以,现在我改用exec:exec因为我们可以设置 workingDirectory 并且它'OK。

<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
    <execution>
        <goals>
            <goal>exec</goal>
        </goals>
    </execution>
</executions>
<configuration> 
    <executable>java</executable>
    <arguments>
        <argument>-classpath</argument> 
        <classpath />
        <argument>com.package.MyMainClass</argument>  
    </arguments>
    <workingDirectory>${project.build.outputDirectory}</workingDirectory>           
</configuration>

回答by Hesham

I couldn't either find a working fix, however an ugly workaround that was applicable in my case is to simply pass ${project.build.directory}(or any maven property for the matter) as an argument to the main class and handling it from there.

我也找不到有效的修复程序,但是适用于我的情况的一个丑陋的解决方法是简单地将${project.build.directory}(或任何 maven 属性)作为参数传递给主类并从那里处理它。

<configuration>
    [...]
    <arguments>
        <argument>${project.build.directory}</argument>
    </arguments>
    [...]
</configuration>

Setting the current working directory inside the code to properly simulate the non-working workingDirectoryconfiguration is a bit tricky if you insist doing so, check thislinked answer for more information.

workingDirectory如果您坚持这样做,在代码中设置当前工作目录以正确模拟非工作配置有点棘手,请查看链接的答案以获取更多信息。