Skip to content

Commit

Permalink
Create GenerateModulesList Mojo and class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolai Parlog authored and gunnarmorling committed May 25, 2021
1 parent 45c954f commit c206bde
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 10 deletions.
37 changes: 37 additions & 0 deletions core/src/main/java/org/moditect/commands/GenerateModuleList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2017 - 2018 The ModiTect authors
*
* 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.moditect.commands;

import org.moditect.model.Version;

import java.nio.file.Path;
import java.util.Set;

public class GenerateModuleList {

private final Version jvmVersion;
private final Set<Path> dependencies;

public GenerateModuleList(Version jvmVersion, Set<Path> dependencies) {
this.jvmVersion = jvmVersion;
this.dependencies = dependencies;
}

public void run() {
System.out.println("Creating module list for " + target + " and " + dependencies);
}

}
63 changes: 63 additions & 0 deletions core/src/main/java/org/moditect/model/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2017 - 2018 The ModiTect authors
*
* 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.moditect.model;

import java.util.Objects;

/**
* Simplified version of Runtime.Version (which requires Java 9+).
*/
public class Version {

private final int feature;

private Version(int feature) {
this.feature = feature;
}

public static Version valueOf(int feature) {
return new Version(feature);
}

public static Version valueOf(Object feature) {
return new Version(Integer.parseInt(String.valueOf(feature)));
}

public int feature() {
return feature;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Version))
return false;
Version version = (Version) o;
return feature == version.feature;
}

@Override
public int hashCode() {
return Objects.hash(feature);
}

@Override
public String toString() {
return "Version " + feature;
}

}
4 changes: 4 additions & 0 deletions integrationtest/undertow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<packaging>jar</packaging>
<name>ModiTect Integration Test - Undertow</name>

<properties>
<moditect.jvmVersion>9</moditect.jvmVersion>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class AddModuleInfoMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.version}", readonly = true, required = true)
private String version;

@Parameter(readonly = true)
@Parameter(property = "moditect.jvmVersion", readonly = true)
private String jvmVersion;

@Parameter(readonly = true, defaultValue = "${project.build.directory}/moditect")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.moditect.commands.CreateRuntimeImage;
import org.moditect.mavenplugin.image.model.Launcher;
import org.moditect.mavenplugin.util.MojoLog;
import org.moditect.mavenplugin.util.DependencyHelper;
import org.moditect.model.JarInclusionPolicy;

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ public class CreateRuntimeImageMojo extends AbstractMojo {
private boolean bindServices;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {
Path jmodsDir = getJModsDir();

Set<Path> effectiveModulePath = this.modulePath.stream()
Expand All @@ -124,7 +125,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
effectiveModulePath,
modules,
jarInclusionPolicy,
getDirectAndTransitiveDependencies(),
DependencyHelper.getDirectAndTransitiveDependencies(project),
project.getArtifact().getFile().toPath(),
launcher != null ? launcher.getName() : null,
launcher != null ? launcher.getModule() : null,
Expand All @@ -146,13 +147,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private Set<Path> getDirectAndTransitiveDependencies() {
return project.getArtifacts()
.stream()
.map(a -> a.getFile().toPath())
.collect(Collectors.toSet());
}

/**
* Returns the directory with the jmod files to be used for creating the image.
* If {@code baseJdk} has been given, the jmod files from the JDK identified that way
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2017 - 2018 The ModiTect authors
*
* 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.moditect.mavenplugin.modulelist;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.moditect.commands.GenerateModuleList;
import org.moditect.mavenplugin.util.DependencyHelper;
import org.moditect.model.Version;

@Mojo(name = "list-application-image-modules",
defaultPhase = LifecyclePhase.PACKAGE,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME
)
public class GenerateModuleListMojo extends AbstractMojo {

@Parameter(defaultValue = "${project}", readonly = true)
protected MavenProject project;

@Parameter(property = "moditect.jvmVersion")
private Integer jvmVersion;

@Override
public void execute() throws MojoExecutionException {
GenerateModuleList generateModuleList = new GenerateModuleList(
determineJvmVersion(),
DependencyHelper.getDirectAndTransitiveDependencies(project)
);
try {
generateModuleList.run();
} catch (RuntimeException ex) {
getLog().error(ex);
throw new MojoExecutionException("Error generating module list", ex);
}
}

private Version determineJvmVersion() {
if (jvmVersion != null) {
return Version.valueOf(jvmVersion);
}

Object rawVersion = project.getProperties().get("maven.compiler.release");
if (rawVersion != null) {
return Version.valueOf(rawVersion);
}

rawVersion = project.getProperties().get("maven.compiler.target");
if (rawVersion != null) {
return Version.valueOf(rawVersion);
}

throw new IllegalStateException("Couldn't determine target version, please specify the 'targetVersion' configuration property");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2017 - 2018 The ModiTect authors
*
* 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.moditect.mavenplugin.util;

import org.apache.maven.project.MavenProject;

import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;

public class DependencyHelper {

public static Set<Path> getDirectAndTransitiveDependencies(MavenProject project) {
return project.getArtifacts()
.stream()
.map(a -> a.getFile().toPath())
.collect(Collectors.toSet());
}

}

0 comments on commit c206bde

Please sign in to comment.