Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload javafx:run goal to work with JavaFX 17 #136

Merged
merged 9 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.2</version>
</dependency>

<!--test-->
<dependency>
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/org/openjfx/JavaFXRunFixMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2021, Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.openjfx;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.shared.invoker.*;
import org.openjfx.model.JavaFXDependency;
import org.openjfx.model.JavaFXModule;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

abhinayagarwal marked this conversation as resolved.
Show resolved Hide resolved
/**
* A temporary mojo introduced to run JavaFX applications
* with Java 17 Maven artifacts.
*/
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.COMPILE)
@Execute(phase = LifecyclePhase.PROCESS_RESOURCES)
public class JavaFXRunFixMojo extends JavaFXBaseMojo {

@Parameter(readonly = true, required = true, defaultValue = "${basedir}/pom.xml")
String pom;
abhinayagarwal marked this conversation as resolved.
Show resolved Hide resolved

// gluonfx-maven-plugin creates `runPom.xml` for gluonfx:runagent goal
@Parameter(readonly = true, required = true, defaultValue = "${basedir}/runPom.xml")
String runpom;

@Parameter(readonly = true, required = true, defaultValue = "${project.basedir}/modifiedPom.xml")
String modifiedPom;

@Parameter(defaultValue = "${session}", readonly = true)
MavenSession session;

@Parameter(defaultValue = "${javafx.platform}", readonly = true)
String javafxPlatform;

@Override
public void execute() throws MojoExecutionException {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
String classifier = "";
if (osName.contains("mac")) {
classifier = "mac";
} else if (osName.contains("nux")) {
classifier = "linux";
} else if (osName.contains("windows")) {
classifier = "win";
} else {
throw new MojoExecutionException("Error, os.name " + osName + " not supported");
}

String PLATFORM = javafxPlatform != null ? javafxPlatform : classifier;

final InvocationRequest invocationRequest = new DefaultInvocationRequest();
invocationRequest.setProfiles(project.getActiveProfiles().stream()
.map(Profile::getId)
.collect(Collectors.toList()));
invocationRequest.setProperties(session.getRequest().getUserProperties());

// 1. Create modified pom
File modifiedPomFile = new File(modifiedPom);
try (InputStream is = new FileInputStream(new File(runpom).exists() ? runpom : pom)) {
// 2. Create model from current pom
Model model = new MavenXpp3Reader().read(is);

Set<JavaFXDependency> javaFXDependencies = new HashSet<>();
List<Dependency> toRemove = new ArrayList<>();
// 3. Check for dependencies
for (Dependency p : model.getDependencies()) {
if (p.getGroupId().equalsIgnoreCase("org.openjfx")) {
toRemove.add(p);
final Optional<JavaFXModule> javaFXModule = JavaFXModule.fromArtifactName(p.getArtifactId());
javaFXModule.ifPresent(module -> {
javaFXDependencies.add(module.getMavenDependency(PLATFORM, p.getVersion()));
javaFXDependencies.addAll(module.getTransitiveMavenDependencies(PLATFORM, p.getVersion()));
});
}
}
model.getDependencies().removeAll(toRemove);
model.getDependencies().addAll(javaFXDependencies);

// 4. Serialize new pom
try (OutputStream os = new FileOutputStream(modifiedPomFile)) {
new MavenXpp3Writer().write(os, model);
}
} catch (Exception e) {
if (modifiedPomFile.exists()) {
modifiedPomFile.delete();
}
throw new MojoExecutionException("Error generating modified pom", e);
}

invocationRequest.setPomFile(modifiedPomFile);
invocationRequest.setGoals(Collections.singletonList("javafx:dorun"));
invocationRequest.setUserSettingsFile(session.getRequest().getUserSettingsFile());

final Invoker invoker = new DefaultInvoker();
try {
final InvocationResult invocationResult = invoker.execute(invocationRequest);
if (invocationResult.getExitCode() != 0) {
throw new MojoExecutionException("Error, javafx:run failed", invocationResult.getExecutionException());
}
} catch (MavenInvocationException e) {
e.printStackTrace();
throw new MojoExecutionException("Error", e);
} finally {
if (modifiedPomFile.exists()) {
modifiedPomFile.delete();
}
}
}
}

8 changes: 7 additions & 1 deletion src/main/java/org/openjfx/JavaFXRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
import static org.openjfx.model.RuntimePathOption.CLASSPATH;
import static org.openjfx.model.RuntimePathOption.MODULEPATH;

@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.RUNTIME)
/**
* Mojo to run a JavaFX application.
*
* Mojo name change from 'run' to 'dorun' is temporary. It will be reverted
* once JavaFX 17.x empty jars are available with Automatic-Module-Name.
*/
@Mojo(name = "dorun", requiresDependencyResolution = ResolutionScope.RUNTIME)
@Execute(phase = LifecyclePhase.PROCESS_CLASSES)
public class JavaFXRunMojo extends JavaFXBaseMojo {

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/openjfx/model/JavaFXDependency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2021, Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.openjfx.model;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Exclusion;

import java.util.ArrayList;
import java.util.List;

public class JavaFXDependency extends Dependency {

public JavaFXDependency(String artifactId, String version) {
setArtifactId(artifactId);
setVersion(version);
setGroupId("org.openjfx");
setExclusions(exclusions());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaFXDependency that = (JavaFXDependency) o;
return this.getArtifactId().equals(that.getArtifactId());
}

private List<Exclusion> exclusions() {
final Exclusion exclusion = new Exclusion();
exclusion.setGroupId("org.openjfx");
exclusion.setArtifactId("*");
List<Exclusion> exclusions = new ArrayList<>();
exclusions.add(exclusion);
return exclusions;
}
}
93 changes: 93 additions & 0 deletions src/main/java/org/openjfx/model/JavaFXModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2021, Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.openjfx.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Stream;

public enum JavaFXModule {

BASE,
GRAPHICS(BASE),
CONTROLS(BASE, GRAPHICS),
FXML(BASE, GRAPHICS),
MEDIA(BASE, GRAPHICS),
SWING(BASE, GRAPHICS),
WEB(BASE, CONTROLS, GRAPHICS, MEDIA);

static final String PREFIX_MODULE = "javafx.";
private static final String PREFIX_ARTIFACT = "javafx-";

private List<JavaFXModule> dependentModules;

JavaFXModule(JavaFXModule...dependentModules) {
this.dependentModules = List.of(dependentModules);
}

public static Optional<JavaFXModule> fromArtifactName(String artifactName) {
return Stream.of(JavaFXModule.values())
.filter(javaFXModule -> artifactName.equals(javaFXModule.getArtifactName()))
.findFirst();
}

public String getModuleName() {
return PREFIX_MODULE + name().toLowerCase(Locale.ROOT);
}

public String getModuleJarFileName() {
return getModuleName() + ".jar";
}

public String getArtifactName() {
return PREFIX_ARTIFACT + name().toLowerCase(Locale.ROOT);
}

public List<JavaFXModule> getDependentModules() {
return dependentModules;
}

public List<JavaFXDependency> getTransitiveMavenDependencies(String platform, String version) {
List<JavaFXDependency> mavenDependencies = new ArrayList<>();
for (JavaFXModule dependentModule : dependentModules) {
mavenDependencies.add(dependentModule.getMavenDependency(platform, version));
}
return mavenDependencies;
}

public JavaFXDependency getMavenDependency(String platform, String version) {
final JavaFXDependency dependency = new JavaFXDependency(getArtifactName(), version);
dependency.setVersion(version);
dependency.setClassifier(platform);
return dependency;
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/openjfx/JavaFXRunMojoTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void testClasspathNoLauncherRun() throws Exception {
}

protected JavaFXRunMojo getJavaFXRunMojo(File testPom) throws Exception {
JavaFXRunMojo mojo = (JavaFXRunMojo) lookupMojo("run", testPom);
JavaFXRunMojo mojo = (JavaFXRunMojo) lookupMojo("dorun", testPom);
assertNotNull(mojo);

setUpProject(testPom, mojo);
Expand Down