I need to create an IntelliJ Maven project using Java code. The command to create new Maven project is:
mvn archetype:generate -DgroupId=ToolsQA -DartifactId=DemoMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
I need to write program code that runs this command through the command line.
Steps:
- run the command line
- enter to C direction
- enter the command that create new maven project.
Thanks!
CodePudding user response:
Make sure the cmd.exe
is in the PATH
, if not, you will need to complete the fully qualified path to it. For example:
ProcessBuilder pb = new ProcessBuilder(
"C:\\Windows\\System32\\cmd.exe",
"/c",
"cd \" C:\\Program Files\" ",
);
Actually, you don't have to use the command to switch execution path, try the directory method:
String initMvnProjectCommand = "mvn archetype:generate ...";
ProcessBuilder builder = new ProcessBuilder(
"C:\\Windows\\System32\\cmd.exe",
"/c",
initMvnProjectCommand);
builder.directory(new File("${the path you want to execute the command}"));
builder.start();
CodePudding user response:
Class ProcessBuilder is not a [Windows] command prompt emulator. Via the class methods, you set a single command to execute as well as the working directory for the command and the environment.
The following code executes the command in your question. It also sets the working directory to C:\Users\USER
. It redirects the command output and the command error output to System.out and System.err, respectively. It does not modify the environment.
I did not add C:\apache-maven-3.8.6\bin
to my [Windows] PATH environment variable and therefore, in the below code, I use the full path to mvn.cmd
.
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("C:\\apache-maven-3.8.6\\bin\\mvn.cmd",
"archetype:generate",
"-DgroupId=ToolsQA",
"-DartifactId=DemoMavenProject",
"-DarchetypeArtifactId=maven-archetype-quickstart",
"-DinteractiveMode=false");
Path path = Paths.get("C:", "Users", "USER");
File directory = path.toFile();
pb.directory(directory);
pb.inheritIO();
try {
Process proc = pb.start();
int status = proc.waitFor();
System.out.println("Completed with status: " status);
}
catch (InterruptedException | IOException x) {
x.printStackTrace();
}
}
}
If the command completes successfully, the value of variable status
should be 0 (zero).
Here is the output I get when I run the above code:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\USER
[INFO] Parameter: package, Value: ToolsQA
[INFO] Parameter: groupId, Value: ToolsQA
[INFO] Parameter: artifactId, Value: DemoMavenProject
[INFO] Parameter: packageName, Value: ToolsQA
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\USER\DemoMavenProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.529 s
[INFO] Finished at: 2022-10-30T20:21:50 02:00
[INFO] ------------------------------------------------------------------------
Completed with status: 0