5

I try to use the exec-maven-plugin to run a Java program.

I use the following pom snippet:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
             <configuration>
                    <executable>java</executable>
                        <arguments>
                         <argument>-Dmyproperty=myvalue</argument>
                            <argument>-cp</argument>
                            <argument>"/home/vogella/libs/*"</argument>
                            <argument>com.vogella.test.Main</argument>
                        </arguments>
    </configuration>


</plugin>

The class com.vogella.test.Main is contained in one of the jar files which are located in /home/vogella/libs/*. If I run the mvn -X clean install exec:exec command, I see the following error message:

[DEBUG] Executing command line: java -Dmyproperty=myvalue -cp "/home/vogella/libs/*" com.vogella.test.Main Error: Could not find or load main class com.vogella.test.Main

If I copy the command line (java -Dmyproperty=myvalue -cp "/home/vogella/libs/*" com.vogella.test.Main) in the shell from which I started the Maven build, then the Java program is executed correctly.

Any idea what is wrong with my Maven setup?

user987339
  • 10,519
  • 8
  • 40
  • 45
vogella
  • 24,574
  • 4
  • 29
  • 26
  • The build is based on the Tycho plug-in, which seens to handle the classpath differently. Default does not work – vogella Jan 06 '14 at 15:03

4 Answers4

4

With CLI, the /home/vogella/libs/* expression is expanded by bash and resolves to the list of files. With Maven, the expression is directly executed and not expanded. so it remains "/home/vogella/libs/*" which is not a valid jar file. You'll probably have more success by using the antrun plugin and use the java Ant task in the script. Ant understands wildcards better than anything else.

Mickael
  • 3,506
  • 1
  • 21
  • 33
  • Thank. Apache Ant works fine. I go with this solution. – vogella Jan 06 '14 at 15:05
  • 1
    I don't think what you're saying is right. It's the java executable which understands classpath set like foo/*, starting with Java 1.6. Lars' problem has probably more to do with incorrect assumptions on what the exec-maven-plugin can do. I understand that exec-maven-plugin allows you to start a java class from the classpath of the maven project. That is, it will handle the classpath. Adding a -cp probably messes things up. – Laurent Petit Jan 06 '14 at 15:45
  • FWIW, I found the following page explaining options to better manage the classpath: http://mojo.codehaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html . None of the options allows to specify jars outside what's in the dependencies of the project. – Laurent Petit Jan 06 '14 at 15:52
1

You need to set the classpath through dependencies. With the commandline argument -cp you set the classpath explicitly but this does not work for the maven cp. This is constructed through dependencies.

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includeProjectDependencies>false</includeProjectDependencies>
                <includePluginDependencies>true</includePluginDependencies>
                <mainClass>org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher</mainClass>
                <arguments>
                    <argument>${project.basedir}/src/my/mavenized/GenerateHeroLanguage.mwe2</argument>
                </arguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xtext</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xbase</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>
  • Thanks but the approach seems a bit to complex, just to add several jar dependencies. I go with the ant runner suggestion. – vogella Jan 06 '14 at 15:04
0

As mentioned in previous answers, Maven doesn't handle well the wild cards. You should either follow the "maven way" and pass them one by one or you can try using the "commandlineArgs" instead.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • As far as I can see, commandlineArgs is not supported by exec-maven-plugin. At least I get error messages if I try to use it. – vogella Jan 06 '14 at 15:04
0

I am not sure if this would work, but maybe you could try again using:

<commandlineArgs> 

instead. I came across this JIRA issue related to it and it sounds like what you need.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Craig
  • 2,286
  • 3
  • 24
  • 37