ca.vanzyl.provisio.maven.ForkedMavenInvoker Maven / Gradle / Ivy
/*
* Copyright (C) 2015-2024 Jason van Zyl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ca.vanzyl.provisio.maven;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import javax.inject.Named;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.StreamConsumer;
@Named("forked")
public class ForkedMavenInvoker implements MavenInvoker {
public MavenResult invoke(MavenRequest request) {
MavenResult result = new MavenResult();
File javaHome = new File(System.getProperty("java.home")).getAbsoluteFile();
if (javaHome.getName().equals("jre")) {
javaHome = javaHome.getParentFile();
}
File mavenHome = request.getMavenHome();
Commandline cli = new Commandline();
File exec;
if (File.pathSeparatorChar == ';') {
exec = new File(mavenHome, "bin/mvn.cmd");
if (!exec.exists()) {
exec = new File(mavenHome, "bin/mvn.bat");
}
} else {
exec = new File(mavenHome, "bin/mvn");
}
cli.setExecutable(exec.getAbsolutePath());
cli.addEnvironment("M2_HOME", mavenHome.getAbsolutePath());
cli.addEnvironment("JAVA_HOME", javaHome.getAbsolutePath());
cli.addEnvironment("MAVEN_TERMINATE_CMD", "on");
cli.setWorkingDirectory(request.getWorkDir());
cli.createArg().setValue("-e");
cli.createArg().setValue("-B");
if (request.getPomFile() != null) {
cli.createArg().setValue("-f");
cli.createArg().setValue(request.getPomFile().getPath());
}
for (Map.Entry