Skip to content
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
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ under the License.
<version>3.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- used by maven-plugin-testing-harness, don't give it compile scope! -->
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
Expand Down Expand Up @@ -205,6 +198,16 @@ under the License.
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.RepositorySystem;
Expand Down Expand Up @@ -52,19 +51,22 @@ public abstract class AbstractDeployMojo extends AbstractMojo {
@Parameter(property = "retryFailedDeploymentCount", defaultValue = "1")
private int retryFailedDeploymentCount;

@Component
private RuntimeInformation runtimeInformation;
private final RuntimeInformation runtimeInformation;

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

@Component
protected RepositorySystem repositorySystem;
protected final RepositorySystem repositorySystem;

private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";

private static final String FIXED_MAVEN_VERSION = "3.9.0";

protected AbstractDeployMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
this.runtimeInformation = runtimeInformation;
this.repositorySystem = repositorySystem;
}

/* Setters and Getters */

void failIfOffline() throws MojoFailureException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.deploy;

import javax.inject.Inject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -41,12 +43,14 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.ReaderFactory;
import org.codehaus.plexus.util.xml.WriterFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactType;
Expand Down Expand Up @@ -197,6 +201,11 @@ public class DeployFileMojo extends AbstractDeployMojo {
@Parameter(property = "maven.deploy.file.skip", defaultValue = "false")
private String skip = Boolean.FALSE.toString();

@Inject
protected DeployFileMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
super(runtimeInformation, repositorySystem);
}

void initProperties() throws MojoExecutionException {
if (pomFile == null) {
boolean foundPom = false;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.deploy;

import javax.inject.Inject;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand All @@ -38,6 +40,8 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.repository.RemoteRepository;
Expand Down Expand Up @@ -140,6 +144,11 @@ public class DeployMojo extends AbstractDeployMojo {
@Parameter(defaultValue = "false", property = "allowIncompleteProjects")
private boolean allowIncompleteProjects;

@Inject
protected DeployMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
super(runtimeInformation, repositorySystem);
}

private enum State {
SKIPPED,
DEPLOYED,
Expand Down
127 changes: 45 additions & 82 deletions src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,51 @@
*/
package org.apache.maven.plugins.deploy;

import javax.inject.Inject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.maven.api.plugin.testing.Basedir;
import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.when;

/**
* @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
*/
public class DeployFileMojoTest extends AbstractMojoTestCase {
private static final String LOCAL_REPO = getBasedir() + "/target/local-repo";

private List<String> expectedFiles;

private List<String> fileList;
@MojoTest
class DeployFileMojoTest {

private File remoteRepo;

private AutoCloseable openMocks;

@Mock
@Inject
private MavenSession session;

@InjectMocks
private DeployFileMojo mojo;

public void setUp() throws Exception {
super.setUp();
@BeforeEach
void setUp() throws Exception {
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(getBasedir() + "/target/local-repo")));
when(session.getRepositorySession()).thenReturn(repositorySession);

remoteRepo = new File(getBasedir(), "target/remote-repo");

Expand All @@ -66,37 +71,19 @@ public void setUp() throws Exception {
}
}

@Override
public void tearDown() throws Exception {
super.tearDown();
if (openMocks != null) {
openMocks.close();
}
}

public void testDeployTestEnvironment() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");

AbstractDeployMojo mojo = (AbstractDeployMojo) lookupMojo("deploy-file", testPom);

@Test
@InjectMojo(goal = "deploy-file")
void testDeployTestEnvironment(DeployFileMojo mojo) throws Exception {
assertNotNull(mojo);
}

public void testBasicDeployFile() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");

mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);

openMocks = MockitoAnnotations.openMocks(this);
@Test
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
@Basedir("/unit/deploy-file-test")
void testBasicDeployFile(DeployFileMojo mojo) throws Exception {

assertNotNull(mojo);

DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
when(session.getRepositorySession()).thenReturn(repositorySession);

String groupId = (String) getVariableValueFromObject(mojo, "groupId");

String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
Expand Down Expand Up @@ -155,8 +142,8 @@ public void testBasicDeployFile() throws Exception {
assertEquals("POM was created from deploy:deploy-file", model.getDescription());

// check the remote-repo
expectedFiles = new ArrayList<>();
fileList = new ArrayList<>();
ArrayList<String> expectedFiles = new ArrayList<>();
List<String> fileList = new ArrayList<>();

File repo = new File(remoteRepo, "deploy-file-test");

Expand Down Expand Up @@ -187,21 +174,13 @@ public void testBasicDeployFile() throws Exception {
assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
}

public void testDeployIfClassifierIsSet() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-classifier/plugin-config.xml");

mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);

openMocks = MockitoAnnotations.openMocks(this);
@Test
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
@Basedir("/unit/deploy-file-classifier")
void testDeployIfClassifierIsSet(DeployFileMojo mojo) throws Exception {

assertNotNull(mojo);

DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
when(session.getRepositorySession()).thenReturn(repositorySession);

String classifier = (String) getVariableValueFromObject(mojo, "classifier");

String groupId = (String) getVariableValueFromObject(mojo, "groupId");
Expand Down Expand Up @@ -237,22 +216,13 @@ public void testDeployIfClassifierIsSet() throws Exception {
assertTrue(prodDeployedArtifact.exists());
}

public void testDeployIfArtifactIsNotJar() throws Exception {
File testPom =
new File(getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml");

mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);

openMocks = MockitoAnnotations.openMocks(this);
@Test
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
@Basedir("/unit/deploy-file-artifact-not-jar")
void testDeployIfArtifactIsNotJar(DeployFileMojo mojo) throws Exception {

assertNotNull(mojo);

DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
when(session.getRepositorySession()).thenReturn(repositorySession);

String groupId = (String) getVariableValueFromObject(mojo, "groupId");

String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
Expand All @@ -276,20 +246,13 @@ public void testDeployIfArtifactIsNotJar() throws Exception {
assertTrue(file.exists());
}

public void testDeployFileIfPackagingIsSet() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml");
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);

openMocks = MockitoAnnotations.openMocks(this);
@Test
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
@Basedir("/unit/deploy-file-packaging")
void testDeployFileIfPackagingIsSet(DeployFileMojo mojo) throws Exception {

assertNotNull(mojo);

DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
when(session.getRepositorySession()).thenReturn(repositorySession);

String packaging = (String) getVariableValueFromObject(mojo, "packaging");

String groupId = (String) getVariableValueFromObject(mojo, "groupId");
Expand Down
Loading