After i wrote
mvn -f pom.xml compile exec:java -Dexec.classpathScope=Compile-Dexec.main Class=storm.starter.WordCountTopology
and found this !!
[INFO] One or more required plugin parameters are invalid/missing for
'exec:java'
[0] Inside the definition for plugin 'exec-maven-plugin' specify the
following:
... VALUE
-OR-
on the command line, specify: '-Dstorm.topology=VALUE
If you link your pom.xml then this would be easier. I'm guessing you're using Storm. Have you written your own topologyClass? From the documentation:
topologyClass
The class name of the topology driver (e.g. "com.foo.bar.MyTopology")
Command line override: -Dmaven.storm.topology=
The documentation also gives you the code for your pom but you might want to add exec-maven-plugin to your pom.xml like so:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.domain.yourApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
One thing to note: you need to alter mainClass to match the class in your project that contains the main method you want to execute.
Then you can just run mvn exec:java.
Related
Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".
See the exec maven plugin. You can run Java classes using:
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
1. Edit POM.xml
Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:
<properties>
<exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>
2. Run Command
Now from the terminal, trigger the following command:
mvn clean compile exec:java
NOTE You can pass further arguments via -Dexec.args="xxx" flag.
The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project.
Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.
No need to add new plugin in pom.xml. Just run this command
mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)'
See the maven exec plugin for more usage.
Give the Exec Maven plugin a try
clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.
Don't forget to mention the class in the <properties></properties> section of pom.xml :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.main.class>com.test.service.MainTester</java.main.class>
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>${java.main.class}</mainClass>
</configuration>
</plugin>
STS Run Configuration along with above Maven Setup:
I've tried to follow the configuration from this response on SO to use the jar location in my local repo as a plugin parameter, but it doesn't seem to work. I don't know if this due to a newer Maven version than the response (I'm using Maven 3.2.5).
In my pom.xml, I need to add a javaagent to my surefire plugin definition. The javaagent jar file is a dependency in my project.
I have tried the following:
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<!-- Configuration to use jmockit on IBM J9 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
I was expecting that the ${org.jmockit:jmockit:jar} would be expanded to the location of the jar, but in my mvn console I see the following error:
[ERROR] Command wascmd.exe /X /C "C:\IBM\SDP\jdk\jre\bin\java -javaagent:${org.jmockit:jmockit:jar} -jar C:\dev\Eclipse\rtc-connector\target\surefire\surefirebooter1389906134960134.jar C:\dev\Eclipse\rtc-connector\target\surefire\surefire5488684370604495471tmp C:\dev\Eclipse\rtc-connector\target\surefire\surefire_05402037720997438783tmp"
So obviously the parameter is not getting expanded. I was hoping/expecting to see something like -javaagent:c:\users\eric\.m2\repository\org.jmockit\1.20\jmockit-1.20.jar or something similar.
Is there a clean way I can reference the jar from my dependency in my plugin configuration? I know I can use the dependency-plugin to copy the jar to a known location in my target folder and then point to that, but I was hoping there would be an easier solution that doesn't require the intermediary step.
To be variable expanded need to add maven-dependency-plugin:
<!-- obtain ${*:*:jar} properties -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
You need to set the "argLine" tag value as follow :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<argLine>
-javaagent:"${localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar"
</argLine>
</configuration>
</plugin>
You need to set the .m2 repo path where the jmockit jar is present. It's working for me.
The maven-dependency-plugin contains a goal build-classpath which would solve this problem.
On command line you can do things like this:
mvn dependency:build-classpath -DincludeArtifactIds=testng -DincludeGroupIds=testng
which results in:
C:\repository\org\testng\testng\6.8.21\testng-6.8.21.jar
The resulting classpath can also be put into a property outputProperty ....
This could be configured into pom as well...
I wrote a maven code in netbeans that in included 6 classes:
ColumnComparator.java
IQC.java
Main.java
MultipleLinearRegression.java
Overlap.java
PSResidualReduction.java
I want to compile and run it on linux terminal. I tried:
javac Main.java ColumnComparator.java IQC.java MultipleLinearRegression.java Overlap.java PSResidualReduction.java
and got compilation error. The problem is that in MultipleLinearRegression class, I used jama package to do Matrix computation, but in command line I do not know how I should modify dependencies. Hopefully you guys can help me.
Just use the exec-maven-plugin.
Add these lines to your pom.xml (you might already have the <build/> and <plugins/> tag there). Make sure to set the <mainClass/> tag to point to your specific main class.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Then you run your program from command line:
mvn exec:java
This will not affect/change the behavior of your Netbeans setup so you will still be able to run the program from within Netbeans.
I'm trying to release a library using Maven and perform a site-deploy to sourceforge (I have create an interactive shell first). The release is done by a Jenkins job (using the Maven Release Plugin for Jenkins).
I tried:
-X -e -Dresume=false -Dusername=puce release:prepare release:perform -Darguments="-Dusername=puce"
and
-X -e -Dresume=false -Dusername=puce -Darguments=-Dusername=puce release:prepare release:perform
but both times the job hangs at site:deploy of the first module:
[INFO] --- maven-site-plugin:3.2:deploy (default-deploy) # myproject-parent ---
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
Using private key: /opt/jenkins/.ssh/id_dsa
When I stop the job, the following gets printed at end:
Password for ${username}#shell.sourceforge.net: channel stopped
which probably means that ${username} wasn't resolved.
How can I resolve the ${username}?
Edit:
Note that the following runs fine:
site-deploy -Psonatype-oss-release -Dusername=puce
Edit 2:
As part of release:perform maven executes the following command:
/usr/share/maven/bin/mvn -s /tmp/release-settings7797889802430474959.xml deploy site-deploy --no-plugin-updates --batch-mode -Psonatype-oss-release -P nexus -f pom.xml
-Dusername=puce doesn't seem to get passed to this maven command...
Also note that help:effective-pom shows the following maven-release-plugin configuration:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
So 'arguments' gets defined and its value seems to reach the embedded maven command instead of the value passed on the command line...
What I've successfully done in the past is as follows:
Define a property in the POM file, e.g.:
<properties>
<release.arguments></release.arguments>
</properties>
Add the POM property to the plugin configuration in the POM file, e.g.;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<arguments>${release.arguments}</arguments>
...
Pass the argument through the property on the command-line, e.g.:
mvn release:prepare -Drelease.arguments="-N -Prelease"
Hope this helps.
Overriding
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
with
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release -Dusername=${username}</arguments>
</configuration>
</plugin>
in one of the parents did the trick.
It seems to be a bug that the value on the command line doesn't override the value in the POM.
My solution was similar to Sander Verhagen's. I added only line only though.
How I run:
mvn --batch-mode release:prepare -Denvironment=production
My config:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tag>${artifactId}-${version}-${environment}</tag>
<arguments>-Denvironment=${environment}</arguments>
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
</plugins>
</build>
The difference is that now my submodules use the variable environment and I don't need to define it twice (I.e. -Darguments=-Denvironment=production -Denvironment=production). I also gives me the flexibility of not adding the properties tag.
I'm implementing a simple RMI server and client. I wanted to speed up the tedious task of adding server codebase each time (lots of terminal-bloating text), so I decided to use the maven exec plugin. Here's how a part of my pom.xml looks now:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<argument>/media/files/EclipseWorkspace/JavaSE/rozprochy/lab2/RmiServer/target/classes</argument>
<argument>-Djava.rmi.server.codebase=file:/media/files/EclipseWorkspace/JavaSE/rozprochy/lab2/RmiServer/target/classes/</argument>
<argument>engine.ComputeEngine</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
Everything's fine when i run mvn exec:exec in the console. The problem arises when I want to let the user specify the rmiregistry port for instance as an argument to the program. Basically, I'd like to add extra arguments from console, in addition to those specified in the POM file. All the solutions I've found overwrote the hardcoded args, when specifying new args from console, and this undesirable. Is it possible to do this somehow?
This is kind of a twisted workaround but I couldn't think of any other way to achieve what you want
Define a property in your pom with the default value for your additional parameter
<properties>
<extra.argument.from.console>extra.argument.from.console.default.value</extra.argument.from.console>
</properties>
In your execution add that property as an argument
<argument>${extra.argument.from.console}</argument>
When invoking maven give value to that property if you don't want to use the default value
mvn exec:exec -Dextra.argument.from.console=value.you.want