Skip to content

Commit

Permalink
Switch to @Inject (#223)
Browse files Browse the repository at this point in the history
* Switch to @Inject
  • Loading branch information
elharo authored Dec 5, 2024
1 parent 7caf7bf commit 78d2a79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 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,8 +71,11 @@ public abstract class AbstractJLinkMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Component
private ToolchainManager toolchainManager;
private final ToolchainManager toolchainManager;

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

/**
* Overload this to produce a zip with another classifier, for example a jlink-zip.
Expand Down
35 changes: 24 additions & 11 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,21 +368,33 @@ 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")
private MavenResourcesFiltering mavenResourcesFiltering;
/**
* Convenience interface for plugins to add or replace artifacts and resources on projects.
*/
private final MavenProjectHelper projectHelper;

private final MavenResourcesFiltering mavenResourcesFiltering;

private final 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@

public class JLinkMojoTest {

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

@BeforeEach
public void setUp() throws NoSuchFieldException, IllegalAccessException {
// given
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 @@ -31,11 +31,12 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class MultipleLauncherTest {

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

@Test
void testSingleLauncher() throws Exception {
// It's OK to specify one launcher with "<launcher>"
// given
JLinkMojo mojo = new JLinkMojo();
// It's OK to specify one launcher with "<launcher>" given
Field launcher = mojo.getClass().getDeclaredField("launcher");
launcher.setAccessible(true);
launcher.set(mojo, "l=com.example.Launch");
Expand All @@ -50,8 +51,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 @@ -66,11 +65,9 @@ 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"));
Field launchers = mojo.getClass().getDeclaredField("launchers");
launchers.setAccessible(true);
launchers.set(mojo, List.of("l1=com.example.Launch1", "l2=com.example.Launch2"));

// when
List<String> jlinkArgs = mojo.createJlinkArgs(List.of(), List.of());
Expand All @@ -82,8 +79,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");
Expand Down

0 comments on commit 78d2a79

Please sign in to comment.