views:

152

answers:

2

Hello,

How can I execute java program during the build, or after the build has just finished, is it possible to do this directly from pom:

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main

EDIT

Lets say I wanna execute org.eclipse.content.MyClass how would I needed to write code?

This builds the project but it doesnt execute my class :

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>deploy</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.eclipse.content.MyClass</mainClass>
                </configuration>
            </plugin>
+1  A: 

Try the exec maven plugin... Oops, I should've read more carefully :-(

Péter Török
@Péter Török I updated my question
Gandalf StormCrow
+2  A: 

Configure maven-exec-plugin with your pom

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <phase>yourPhase</phase>
        ...
        <goals>
          <goal>java</goal>
        </goals>
           <configuration>
             <mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass>
          </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

In line <phase>yourPhase</phase> insert maven phase in what this plugin should run.

This class need to be avaliable in pom classpath (as source or as a dependency). In other case if it shouldn't be a project dependency read this article how to configure a exec plugin.

cetnar
@cetnar what about this ? `<phase>yourPhase</phase>...<goals>` , lets say I wanna execute org.eclipse.content.MyClass how would I needed to write code?
Gandalf StormCrow
@cetnar I updated my question
Gandalf StormCrow
@Gandalf I've updated my answer. Main class and other parameters you can place in configuration section.
cetnar
@cetnar I added mainClass= to the configuration property, stil no result.. maybe I should put the reference to built classes in target instead of org ...
Gandalf StormCrow
@cetnar I added `<id>` before phase and it worked .. but now I get Reason: An exception occured while executing the Java class. mainClass=org.sonatype.mavenbook.weather.Main .. it says I get an exeption.. how can I see that exeption, its running ok as a standalone java file..
Gandalf StormCrow
its alrite its working without mainClass= .. thank you
Gandalf StormCrow
@Gandalf. What happened? Any errors? What do you call? Run `mvn deploy` to see if it works.
cetnar