org.openjfx.JavaFXRunMojo Maven / Gradle / Ivy
/*
* Copyright 2019 Gluon
*
* 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 org.openjfx;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.Executor;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class JavaFXRunMojo extends JavaFXBaseMojo {
/**
*
* The executable. Can be a full path or the name of the executable. In the latter case, the executable must be in
* the PATH for the execution to work.
*
*/
@Parameter(property = "javafx.executable", defaultValue = "java")
private String executable;
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info( "skipping execute as per configuration" );
return;
}
if (executable == null) {
throw new MojoExecutionException("The parameter 'executable' is missing or invalid");
}
if (basedir == null) {
throw new IllegalStateException( "basedir is null. Should not be possible." );
}
try {
handleWorkingDirectory();
List commandArguments = new ArrayList<>();
handleArguments(commandArguments);
Map enviro = handleSystemEnvVariables();
CommandLine commandLine = getExecutablePath(executable, enviro, workingDirectory);
String[] args = commandArguments.toArray(new String[commandArguments.size()]);
commandLine.addArguments(args, false);
getLog().debug("Executing command line: " + commandLine);
Executor exec = new DefaultExecutor();
exec.setWorkingDirectory(workingDirectory);
try {
int resultCode;
if (outputFile != null) {
if ( !outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) {
getLog().warn( "Could not create non existing parent directories for log file: " + outputFile );
}
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(outputFile);
resultCode = executeCommandLine(exec, commandLine, enviro, outputStream);
} finally {
IOUtil.close(outputStream);
}
} else {
resultCode = executeCommandLine(exec, commandLine, enviro, System.out, System.err);
}
if (resultCode != 0) {
String message = "Result of " + commandLine.toString() + " execution is: '" + resultCode + "'.";
getLog().error(message);
throw new MojoExecutionException(message);
}
} catch (ExecuteException e) {
getLog().error("Command execution failed.", e);
e.printStackTrace();
throw new MojoExecutionException("Command execution failed.", e);
} catch (IOException e) {
getLog().error("Command execution failed.", e);
throw new MojoExecutionException("Command execution failed.", e);
}
} catch (Exception e) {
throw new MojoExecutionException("Error", e);
}
}
private void handleArguments(List commandArguments) throws MojoExecutionException, MojoFailureException {
preparePaths();
if (options != null) {
options.stream()
.filter(Objects::nonNull)
.filter(String.class::isInstance)
.map(String.class::cast)
.forEach(commandArguments::add);
}
if (modulepathElements != null && !modulepathElements.isEmpty()) {
commandArguments.add(" --module-path");
String modulePath = StringUtils.join(modulepathElements.iterator(), File.pathSeparator);
commandArguments.add(modulePath);
commandArguments.add(" --add-modules");
if (moduleDescriptor != null) {
commandArguments.add(" " + moduleDescriptor.name());
} else {
String modules = pathElements.values().stream()
.filter(Objects::nonNull)
.map(JavaModuleDescriptor::name)
.filter(Objects::nonNull)
.filter(module -> module.startsWith(JAVAFX_PREFIX) && !module.endsWith("Empty"))
.collect(Collectors.joining(","));
commandArguments.add(" " + modules);
}
}
if (classpathElements != null && !classpathElements.isEmpty()) {
commandArguments.add(" -classpath");
String classpath = "";
if (moduleDescriptor != null) {
classpath = project.getBuild().getOutputDirectory() + File.pathSeparator;
}
classpath += StringUtils.join(classpathElements.iterator(), File.pathSeparator);
commandArguments.add(classpath);
}
if (mainClass != null) {
if (moduleDescriptor != null) {
commandArguments.add(" --module");
if (!mainClass.startsWith(moduleDescriptor.name() + "/")) {
commandArguments.add(" " + moduleDescriptor.name() + "/" + mainClass);
} else {
commandArguments.add(" " + mainClass);
}
} else {
commandArguments.add(" " + mainClass);
}
}
if (commandlineArgs != null) {
commandArguments.add(commandlineArgs);
}
}
// for tests
void setExecutable(String executable) {
this.executable = executable;
}
void setBasedir(File basedir) {
this.basedir = basedir;
}
void setCommandlineArgs(String commandlineArgs) {
this.commandlineArgs = commandlineArgs;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy