Skip to content

Commit

Permalink
Switch to @Inject
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 2, 2024
1 parent 022d9e5 commit d788b1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
Expand All @@ -72,9 +71,12 @@ public abstract class AbstractJLinkMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Component
private ToolchainManager toolchainManager;

public AbstractJLinkMojo(ToolchainManager toolchainManager) {
this.toolchainManager = toolchainManager;
}

/**
* Overload this to produce a zip with another classifier, for example a jlink-zip.
* @return get the classifier.
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* under the License.
*/

import javax.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -67,6 +69,7 @@
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
import org.codehaus.plexus.archiver.Archiver;
Expand All @@ -89,8 +92,6 @@
*/
@Mojo(name = "jlink", requiresDependencyResolution = ResolutionScope.RUNTIME, defaultPhase = LifecyclePhase.PACKAGE)
public class JLinkMojo extends AbstractJLinkMojo {
@Component
private LocationManager locationManager;

/**
* <p>
Expand Down Expand Up @@ -367,22 +368,34 @@ public class JLinkMojo extends AbstractJLinkMojo {
private String outputTimestamp;

/**
* Convenience interface for plugins to add or replace artifacts and resources on projects.
*/
@Component
private MavenProjectHelper projectHelper;

/**
* These file are added to the image after calling the jlink, but before creating the zipfile.
* These files are added to the image after calling the jlink, but before creating the zipfile.
*
* @since 3.2.0
*/
@Parameter
private List<Resource> additionalResources;

@Component(role = MavenResourcesFiltering.class, hint = "default")
/**
* Convenience interface for plugins to add or replace artifacts and resources on projects.
*/
private MavenProjectHelper projectHelper;

private MavenResourcesFiltering mavenResourcesFiltering;

private LocationManager locationManager;

@Inject
public JLinkMojo(
MavenProjectHelper projectHelper,
ToolchainManager toolchainManager,
MavenResourcesFiltering mavenResourcesFiltering,
LocationManager locationManager) {
super(toolchainManager);
this.mavenResourcesFiltering = mavenResourcesFiltering;
this.projectHelper = projectHelper;
this.locationManager = locationManager;
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
failIfParametersAreNotInTheirValidValueRanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JLinkMojoTest {
@Test
void quote_every_argument() throws Exception {
// given
JLinkMojo mojo = new JLinkMojo();
JLinkMojo mojo = new JLinkMojo(null, null, null, null);
Field stripDebug = mojo.getClass().getDeclaredField("stripDebug");
stripDebug.setAccessible(true);
stripDebug.set(mojo, Boolean.TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,26 @@
import java.util.Set;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class MultipleLauncherTest {

private JLinkMojo mojo = new JLinkMojo(null, null, null, null);
private Field launcher;

@BeforeEach
public void setUp() throws NoSuchFieldException, IllegalAccessException {
// It's OK to specify one launcher with "<launcher>" given
launcher = mojo.getClass().getDeclaredField("launcher");
launcher.setAccessible(true);
}

@Test
void testSingleLauncher() throws Exception {
// It's OK to specify one launcher with "<launcher>"
// given
JLinkMojo mojo = new JLinkMojo();
Field launcher = mojo.getClass().getDeclaredField("launcher");
launcher.setAccessible(true);
launcher.set(mojo, "l=com.example.Launch");

// when
Expand All @@ -49,9 +56,6 @@ void testSingleLauncher() throws Exception {

@Test
void testOneMultipleLauncher() throws Exception {
// It's OK to specify one launcher with "<launchers>"
// given
JLinkMojo mojo = new JLinkMojo();
Field launchers = mojo.getClass().getDeclaredField("launchers");
launchers.setAccessible(true);
launchers.set(mojo, List.of("l=com.example.Launch"));
Expand All @@ -65,11 +69,6 @@ void testOneMultipleLauncher() throws Exception {

@Test
void testMultipleLaunchers() throws Exception {
// It's OK to specify multiple launchers with the "<launchers>" element
// given
JLinkMojo mojo = new JLinkMojo();
Field launcher = mojo.getClass().getDeclaredField("launchers");
launcher.setAccessible(true);
launcher.set(mojo, List.of("l1=com.example.Launch1", "l2=com.example.Launch2"));

// when
Expand All @@ -81,11 +80,6 @@ void testMultipleLaunchers() throws Exception {

@Test
void testInvalidLauncherConfig() throws Exception {
// It's an error to specify both "<launcher>" and "<launchers>"
// given
JLinkMojo mojo = new JLinkMojo();
Field launcher = mojo.getClass().getDeclaredField("launcher");
launcher.setAccessible(true);
launcher.set(mojo, "l3=com.example.Launch3");
Field launchers = mojo.getClass().getDeclaredField("launchers");
launchers.setAccessible(true);
Expand Down

0 comments on commit d788b1f

Please sign in to comment.