21

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

mvn exec:java

and I didn't manage to set the working directory with 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}.

Thank you for your help.

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

<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

[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,
user1842947
  • 1,047
  • 1
  • 11
  • 16

3 Answers3

28

I didn't found solution with exec:java.

So, now I use exec:exec instead because we can set the workingDirectory and it' 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>
user1842947
  • 1,047
  • 1
  • 11
  • 16
  • pitfall: if the directory doesn't exists it fallsback to base dir without giving any info! so check it with `mvn -X verify | grep working` or similar – estani Oct 26 '18 at 11:54
3

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.

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

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

Community
  • 1
  • 1
Hesham Saleh
  • 439
  • 1
  • 4
  • 11
1

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

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

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

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Thank you, but even if I've added this -Dexec option it doesn't work for me, my Main class doesn't find files in my workingdir. I've pasted the result of the mvn exec:java -X command – user1842947 Aug 21 '14 at 17:26
  • What kind of files does your main class searching for ? property files? – khmarbaise Aug 21 '14 at 19:45
  • My Java program need a dll in the running folder. This file is in the right place but I'm still getting the same error : missing dll. Otherwise I've used the surefire plugin for my Junit tests and it works fine for me, the workingDirectory target works – user1842947 Aug 21 '14 at 19:59
  • 5
    Property exec.workingdir can be used only when executing via exec:exec. – vasekt Apr 22 '15 at 07:39