use of org.apache.maven.shared.invoker.Invoker in project maven-plugins by apache.
the class JavadocUtil method invokeMaven.
/**
* Invoke Maven for the given project file with a list of goals and properties, the output will be in the
* invokerlog file.
* <br/>
* <b>Note</b>: the Maven Home should be defined in the <code>maven.home</code> Java system property or defined in
* <code>M2_HOME</code> system env variables.
*
* @param log a logger could be null.
* @param localRepositoryDir the localRepository not null.
* @param projectFile a not null project file.
* @param goals a not null goals list.
* @param properties the properties for the goals, could be null.
* @param invokerLog the log file where the invoker will be written, if null using <code>System.out</code>.
* @throws MavenInvocationException if any
* @since 2.6
*/
protected static void invokeMaven(Log log, File localRepositoryDir, File projectFile, List<String> goals, Properties properties, File invokerLog) throws MavenInvocationException {
if (projectFile == null) {
throw new IllegalArgumentException("projectFile should be not null.");
}
if (!projectFile.isFile()) {
throw new IllegalArgumentException(projectFile.getAbsolutePath() + " is not a file.");
}
if (goals == null || goals.size() == 0) {
throw new IllegalArgumentException("goals should be not empty.");
}
if (localRepositoryDir == null || !localRepositoryDir.isDirectory()) {
throw new IllegalArgumentException("localRepositoryDir '" + localRepositoryDir + "' should be a directory.");
}
String mavenHome = getMavenHome(log);
if (StringUtils.isEmpty(mavenHome)) {
String msg = "Could NOT invoke Maven because no Maven Home is defined. You need to have set the M2_HOME " + "system env variable or a maven.home Java system properties.";
if (log != null) {
log.error(msg);
} else {
System.err.println(msg);
}
return;
}
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(mavenHome));
invoker.setLocalRepositoryDirectory(localRepositoryDir);
InvocationRequest request = new DefaultInvocationRequest();
request.setBaseDirectory(projectFile.getParentFile());
request.setPomFile(projectFile);
if (log != null) {
request.setDebug(log.isDebugEnabled());
} else {
request.setDebug(true);
}
request.setGoals(goals);
if (properties != null) {
request.setProperties(properties);
}
File javaHome = getJavaHome(log);
if (javaHome != null) {
request.setJavaHome(javaHome);
}
if (log != null && log.isDebugEnabled()) {
log.debug("Invoking Maven for the goals: " + goals + " with " + (properties == null ? "no properties" : "properties=" + properties));
}
InvocationResult result = invoke(log, invoker, request, invokerLog, goals, properties, null);
if (result.getExitCode() != 0) {
String invokerLogContent = readFile(invokerLog, "UTF-8");
// see DefaultMaven
if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
if (log != null) {
log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS...");
if (log.isDebugEnabled()) {
log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS...");
}
}
result = invoke(log, invoker, request, invokerLog, goals, properties, "");
}
}
if (result.getExitCode() != 0) {
String invokerLogContent = readFile(invokerLog, "UTF-8");
// see DefaultMaven
if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
throw new MavenInvocationException(ERROR_INIT_VM);
}
throw new MavenInvocationException("Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
}
}
use of org.apache.maven.shared.invoker.Invoker in project maven-plugins by apache.
the class SiteInvoker method invoke.
/**
* @param projectFile not null, should be in the ${project.basedir}
* @param invokerLog not null
* @param mavenHome not null
* @param goals the list of goals
* @param properties the properties for the invoker
*/
private void invoke(File projectFile, File invokerLog, String mavenHome, List goals, List activeProfiles, Properties properties) {
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(mavenHome));
File localRepoDir = new File(localRepository.getBasedir());
invoker.setLocalRepositoryDirectory(localRepoDir);
InvocationRequest request = new DefaultInvocationRequest();
request.setLocalRepositoryDirectory(localRepoDir);
// request.setUserSettingsFile( settingsFile );
request.setBatchMode(true);
request.setShowErrors(getLog().isErrorEnabled());
request.setDebug(getLog().isDebugEnabled());
// request.setShowVersion( false );
request.setBaseDirectory(projectFile.getParentFile());
request.setPomFile(projectFile);
request.setGoals(goals);
request.setProperties(properties);
request.setProfiles(activeProfiles);
File javaHome = getJavaHome();
if (javaHome != null) {
request.setJavaHome(javaHome);
}
InvocationResult invocationResult;
try {
if (getLog().isDebugEnabled()) {
getLog().debug("Invoking Maven for the goals: " + goals + " with properties=" + properties);
}
invocationResult = invoke(invoker, request, invokerLog, goals, properties, null);
} catch (MavenInvocationException e) {
getLog().error("Error when invoking Maven, consult the invoker log.");
getLog().debug(e);
return;
}
String invokerLogContent = null;
Reader reader = null;
try {
reader = ReaderFactory.newReader(invokerLog, "UTF-8");
invokerLogContent = IOUtil.toString(reader);
reader.close();
reader = null;
} catch (IOException e) {
getLog().error("IOException: " + e.getMessage());
getLog().debug(e);
} finally {
IOUtil.close(reader);
}
if (invokerLogContent != null && invokerLogContent.contains("Error occurred during initialization of VM")) {
getLog().info("Error occurred during initialization of VM, try to use an empty MAVEN_OPTS.");
if (getLog().isDebugEnabled()) {
getLog().debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS");
}
try {
invocationResult = invoke(invoker, request, invokerLog, goals, properties, "");
} catch (MavenInvocationException e) {
getLog().error("Error when reinvoking Maven, consult the invoker log.");
getLog().debug(e);
return;
}
}
if (invocationResult.getExitCode() != 0) {
if (getLog().isErrorEnabled()) {
getLog().error("Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
}
}
}
Aggregations