43

My Java EE proj builds fine, but when trying to execute get following error:

gert@gert-VirtualBox:~/workspace/CDBOOKSTORE$ mvn exec:java 
[INFO] Scanning for projects... 
[INFO]                                       
[INFO] ------------------------------------------------------------------------
[INFO] Building CDBOOKSTORE 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO]  
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ CDBOOKSTORE >>>    
[INFO]  
[INFO] --- maven-dependency-plugin:2.8:copy (default) @ CDBOOKSTORE ---
[INFO]  
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ CDBOOKSTORE <<<    
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ CDBOOKSTORE ---
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.505s [INFO] Finished at: Fri Nov 08 14:01:40 EST 2013 
[INFO] Final Memory: 9M/21M 
[INFO] ------------------------------------------------------------------------    
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project CDBOOKSTORE: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java are missing or invalid
-> [Help 1]

But in my pom.xml file (which I've edited following a Java EE book example), I have specified the mainClass config:

 <?xml version="1.0" encoding="UTF-8"?> 
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>

     <groupId>org.aptovia.javaee7</groupId>
     <artifactId>CDBOOKSTORE</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>app-client</packaging>

     <name>CDBOOKSTORE</name>

     <properties>
         <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>

     <dependencies>
         <dependency>
             <groupId>javax</groupId>
             <artifactId>javaee-api</artifactId>
             <version>7.0</version>
             <scope>provided</scope>
         </dependency>

         <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>2.1.0.Final</version>
         </dependency>

     </dependencies>

     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.1</version>
                 <configuration>
                     <source>1.7</source>
                     <target>1.7</target>
                     <compilerArguments>
                         <endorseddirs>${endorsed.dir}</endorseddirs>
                     </compilerArguments>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-acr-plugin</artifactId>
                 <version>1.0</version>
                 <extensions>true</extensions>
                 <configuration>
                     <archive>
                         <manifest>                            
                             <mainClass>org.aptovia.javaee7.CDBOOKSTORE.Main</mainClass>
                         </manifest>
                     </archive>
                 </configuration>
             </plugin>

             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-dependency-plugin</artifactId>
                 <version>2.8</version>
                 <executions>
                     <execution>
                         <phase>validate</phase>
                         <goals>
                             <goal>copy</goal>
                         </goals>
                         <configuration>
                             <outputDirectory>${endorsed.dir}</outputDirectory>
                             <silent>true</silent>
                             <artifactItems>
                                 <artifactItem>
                                     <groupId>javax</groupId>
                                     <artifactId>javaee-endorsed-api</artifactId>
                                     <version>7.0</version>
                                     <type>jar</type>
                                 </artifactItem>
                             </artifactItems>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>

              <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>                         
                            <mainClass>org.aptovia.javaee7.CDBOOKSTORE.Main</mainClass>
                        </configuration>
                    </execution>
                </executions>
             </plugin>

         </plugins>
     </build>

 </project>

Also looks OK according to this: https://www.mojohaus.org/exec-maven-plugin/usage.html

I've done an mvn clean, rebuilt, but still getting this error. Really not sure whats happening :(

Any help much appreciated!

Archangel1C
  • 100
  • 11
gvanto
  • 1,936
  • 3
  • 21
  • 26
  • Note: the '**' around the mainClass attribute are inserted be stackoverflow ... (not in my pom.xml) – gvanto Nov 08 '13 at 04:15
  • If you get this with a "spring boot" application, see https://stackoverflow.com/q/27323104/32453 (apparently you can't use exec.mainClass with spring boot...) – rogerdpack May 07 '18 at 21:21

6 Answers6

48

"configuration" must go outside "executions", like this:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>com.emilio.App</mainClass>
        </configuration>
    </plugin>
</plugins>
fdermishin
  • 3,519
  • 3
  • 24
  • 45
bronson
  • 489
  • 4
  • 3
  • 6
    This worked for me, but I'm surprised because I've got another project where the `configuration` works fine within an `execution` block. – Leif Gruenwoldt May 12 '14 at 16:17
  • @LeifGruenwoldt If you're referring to the project I think you're referring to - it doesn't work. – SeanIdzenga Jun 16 '20 at 14:56
  • This works, but in case you have arguments to pass on to the main class, pls check out my answer below ! Cant post the entire thing in comment section, due to character count restriction. – wingsforever Dec 29 '22 at 02:00
11

Make sure you execute the mvn exec:java from within the same directory where your pom.xml with exec configuration resides.

George
  • 7,206
  • 8
  • 33
  • 42
5

Configuration seems correct with missing id parameter inside execution part. Proper way would be:

<executions>
    <execution>
        <id>someID</id>
        <goals>
            <goal>java</goal>
        </goals>
        <configuration>
            <mainClass>org.aptovia.javaee7.CDBOOKSTORE.Main</mainClass>
        </configuration>
    </execution>
</executions>

Assuming you are running main method using maven, execution would be like below:

mvn exec:java@someID

someID added inside execution part.

Vaibs
  • 2,018
  • 22
  • 29
5

If the <configuration> is outside the <executions> , it is the configuration for the plugin to be used no matter what the life-cycle phase is.

If the <configuration> is inside the <executions>, it is the configuration to be used in certain life-cycle phase.

As Vabis has suggested, use mvn exec:java@someID to execute the specific execution of the goal for a plugin.

For more information, please see https://maven.apache.org/guides/mini/guide-configuring-plugins.html.

CatSleeping
  • 81
  • 1
  • 3
1

In case you have arguments to pass on to the main class, then the below structure helps.

<build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>org.rraj.server.EchoServer</mainClass>
          <arguments>
            <argument>8585</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>

Pls upvote if you find this answer helpful :)

wingsforever
  • 484
  • 7
  • 15
-3

There is no such parameter like mainClass in exec-maven-plugin, try to use:

<configuration>
   <executable>java</executable>
   <arguments>
     <argument>-classpath</argument>
     <classpath/>
     <argument>org.aptovia.javaee7.CDBOOKSTORE.Main</argument>
   </arguments>
</configuration>
ragnor
  • 2,498
  • 1
  • 22
  • 25
  • Thanks ragnor but this doesn't work tried it. The documentation says there's a parameter mainClass: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html – gvanto Nov 11 '13 at 01:19
  • I've somehow overlooked the parameter in the doc. Does it change anything if you change **packaging** from `app-client` to `jar`? Could you also try to invoke: `mvn exec:java -X -Dexec.mainClass="org.aptovia.javaee7.CDBOOKSTORE.Main" -Ddetail=true` – ragnor Nov 12 '13 at 07:07
  • 1
    I gave up on GlassFish, now trying with JBoss but running into problems again: (oh the joy!): http://stackoverflow.com/questions/20085259/maven-build-failure-for-jboss-as-7-hello-world-example – gvanto Nov 20 '13 at 00:45