How to Run Maven from Java

run main class of Maven project

Try the maven-exec-plugin. From there:

mvn exec:java -Dexec.mainClass="com.example.Main"

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.

If you're on Windows, apply quotes for exec.mainClass and exec.args:

mvn exec:java -D"exec.mainClass"="com.example.Main"

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<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.example.Main</mainClass>
<arguments>
<argument>foo</argument>
<argument>bar</argument>
</arguments>
</configuration>
</plugin>

How to run a mvn command from a java program?

I managed to run the mvn using the following code:
(I use this command: Runtime.getRuntime().exec(cmd);)

    import java.io.*;
import java.util.ArrayList;

public class RunMvnFromJava {
static public String[] runCommand(String cmd)throws IOException
{

// The actual procedure for process execution:
//runCommand(String cmd);
// Create a list for storing output.
ArrayList list = new ArrayList();
// Execute a command and get its process handle
Process proc = Runtime.getRuntime().exec(cmd);
// Get the handle for the processes InputStream
InputStream istr = proc.getInputStream();
// Create a BufferedReader and specify it reads
// from an input stream.

BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String str; // Temporary String variable
// Read to Temp Variable, Check for null then
// add to (ArrayList)list
while ((str = br.readLine()) != null)
list.add(str);
// Wait for process to terminate and catch any Exceptions.
try {
proc.waitFor();
}
catch (InterruptedException e) {
System.err.println("Process was interrupted");
}
// Note: proc.exitValue() returns the exit value.
// (Use if required)
br.close(); // Done.
// Convert the list to a string and return
return (String[])list.toArray(new String[0]);
}
// Actual execution starts here
public static void main(String args[]) throws IOException
{
try
{
// Run and get the output.
String outlist[] = runCommand("mvn integration-test -DskipTests -P interactive -e");
// Print the output to screen character by character.
// Safe and not very inefficient.
for (int i = 0; i < outlist.length; i++)
System.out.println(outlist[i]);
}
catch (IOException e) {
System.err.println(e);
}
}
}

Maven Run Project

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>

Execute Maven plugin command from Java code

It should be

mvn dependency:get

reference

How do I run a java application with maven from command line on ubuntu?

Here, you have maven project which has one dependency on jackson-databind which in turn will have some more dependencies i.e jackson-core and jackson-annotations.

Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application main class from your project directly using java command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.

Since, it is a maven project, these dependent jars will be pulled into maven default directory (.m2) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/, For example your username which you are logged in with is singularli then your home path must be /home/singularli, you can also check it with echo $HOME command.

So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository, now here you would find jars like jackson-databind, jackson-core (these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).

At last, once you find these jars, you would need to specify the classpath using -cp flag and include these jars with your application jar which would look like as given below:

java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application

It should work the same way as shown in that video, you referred in your question.

Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version

Running Java program using mvn through command line

Your program loads classes from the com.solacesystems dependency in your pom.xml, but your classpath only contains your build artifact jar. Build a fat jar, as @Kerry suggests, or use the exec-maven-plugin to run from the command line. From within your project directory (where you execute mvn package), execute:

mvn exec:java -Dexec.mainClass=TopicPublisher

The plugin builds the classpath argument from the dependencies defined in your pom. See https://www.mojohaus.org/exec-maven-plugin/ for more options.



Related Topics



Leave a reply



Submit