Skip to content

Commit

Permalink
SHRINKRES-263: When resolving a .war dependency, search for output fr…
Browse files Browse the repository at this point in the history
…om maven war plugin, not just compiled classes;Check against artifact type property, not extension when determining whether we deal with a war or not.
  • Loading branch information
Florian Besser authored and MatousJobanek committed Jan 26, 2017
1 parent b99d19d commit 16c21f2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build
atlassian-ide-plugin.xml

!impl-maven/src/test/resources/repository/org/jboss/shrinkwrap/test/test-pom/1.0.0/target
!impl-maven/src/test/resources/repository/org/jboss/shrinkwrap/test/test-war-with-resources/1.0.0/target
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.zip.ZipOutputStream;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.resolution.ArtifactResult;
import org.jboss.shrinkwrap.resolver.api.maven.MavenArtifactInfo;
Expand Down Expand Up @@ -140,11 +141,15 @@ private static File artifactToFile(final Artifact artifact) throws IllegalArgume
String extension = artifact.getExtension();
String classifier = artifact.getClassifier();

// SHRINKRES-102, allow test classes to be packaged as well
File root = new File(artifact.getFile().getParentFile(), "target/classes");
if (!Validate.isNullOrEmpty(classifier) && "tests".equals(classifier)) {
// SHRINKRES-102, allow test classes to be packaged as well
root = new File(artifact.getFile().getParentFile(), "target/test-classes");
} else if ("war".equals(artifact.getProperty(ArtifactProperties.TYPE, null))) {
// SHRINKRES-263, allow .war files to be packaged as well
root = new File(artifact.getFile().getParentFile(), "target/" + artifactId + "-" + artifact.getVersion());
}

try {
File archive = File.createTempFile(artifactId + "-", "." + extension);
archive.deleteOnExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.junit.Test;
import org.mockito.Mockito;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;

/**
* This test case simulates behavior of an IDE - in IDE an artifact, that is also another module loaded in the IDE,
* is not fetched from local/remote repository, but is referenced to the location of the module's directory.
Expand Down Expand Up @@ -66,4 +69,37 @@ public void packageDirectoriesWithClasses() throws IOException {
Assert.assertNull(outputZipFile.getEntry("a/non-exist" + File.separator));

}

/**
* Test special logic when resolving a war dependency.
*/
@Test
public void packageWar() throws IOException {
File artifactFile = new File(
System.getProperty("user.dir") + "/target/repository/org/jboss/shrinkwrap/test/test-war-with-resources/1.0.0/pom.xml");

Artifact testPomArtifactMock = Mockito.mock(Artifact.class);
Mockito.when(testPomArtifactMock.getGroupId()).thenReturn("org.jboss.shrinkwrap.test");
Mockito.when(testPomArtifactMock.getArtifactId()).thenReturn("test-war");
Mockito.when(testPomArtifactMock.getExtension()).thenReturn("war");
Mockito.when(testPomArtifactMock.getClassifier()).thenReturn("");
Mockito.when(testPomArtifactMock.getVersion()).thenReturn("1.0.0");
Mockito.when(testPomArtifactMock.getFile()).thenReturn(artifactFile);
Mockito.when(testPomArtifactMock.getProperty(eq(ArtifactProperties.TYPE), anyString())).thenReturn("war");

ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setDependencyNode(new DefaultDependencyNode(new Dependency(testPomArtifactMock, "compile")));
ArtifactResult mockedArtResult = new ArtifactResult(artifactRequest);
mockedArtResult.setArtifact(testPomArtifactMock);

MavenResolvedArtifact mavenResolvedArtifact = MavenResolvedArtifactImpl.fromArtifactResult(mockedArtResult);
ZipFile outputZipFile = new ZipFile(mavenResolvedArtifact.asFile());

//Check if the included files were taken from the "target/Artifact.artifactId-Artifact.version" directory
Assert.assertNotNull(outputZipFile.getEntry("special/a.file"));

//Check if the default "target/classes" directory was not included
Assert.assertNull(outputZipFile.getEntry("a/a.file"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

<!-- Artifact Configuration -->
<groupId>org.jboss.shrinkwrap.test</groupId>
<artifactId>test-war</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

</project>

0 comments on commit 16c21f2

Please sign in to comment.