10

I am trying to run a Java project using Maven, need help on how to run

I tried with various options, Run As > Maven clean, Run As > Maven Install, Run As > Maven test, etc. But the output is not showing in the console though build is successful

I am using eclipse and able to run java file using Run As > Java Application

My Build tag in pom.xml

 <build>
    <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
                <source>1.6</source>
                <target>1.6</target>
        </configuration>
     </plugin>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
       <version>1.6.0</version>
        <configuration>
             <mainClass>mypackage.classnamehavingmain</mainClass>
        </configuration>
        </plugin>
    </plugins>
   </build>
3
  • 2
    Maven is a build tool. It compiles the classes, finds the dependencies, makes a JAR. It doesn't run anything except a suite of tests. You don't run your app via Maven. Commented Sep 17, 2019 at 18:44
  • Thank you Thank you @Michael But I have added an execution plugin in my pom.xml and expecting the java program to run? Is my understanding wrong, Ref: mojohaus.org/exec-maven-plugin/usage.html Commented Sep 17, 2019 at 19:11
  • Not familiar with the plugin, but you have not specified any executions (i.e. the plugin will do nothing). Take a look at their configuration mojohaus.org/exec-maven-plugin/usage.html and copy where appropriate. Running in Maven sounds like a hack though, even if a plugin makes it possible. Commented Sep 17, 2019 at 19:13

2 Answers 2

7

You should add the goal for execution

 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <goals>
              <goal>your_goal_name</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.example.Main</mainClass>
        </configuration>
      </plugin>

and then to execute you can perform :

 mvn exec:your_goal_name
Sign up to request clarification or add additional context in comments.

1 Comment

Adding phase and goal solved my problem <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> </execution>
5

If you would like to run it from the terminal you can do mvn compile exec:java -Dexec.mainClass=mypackage.classnamehavingmain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.