diff --git a/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/EffectivePomMavenDependencyShortcut.java b/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/EffectivePomMavenDependencyShortcut.java index 2bfe37117..8ef58a727 100644 --- a/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/EffectivePomMavenDependencyShortcut.java +++ b/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/EffectivePomMavenDependencyShortcut.java @@ -22,6 +22,7 @@ package org.jboss.shrinkwrap.resolver.api.maven; +import java.io.File; import java.util.Collection; import org.jboss.shrinkwrap.api.GenericArchive; @@ -59,4 +60,23 @@ public interface EffectivePomMavenDependencyShortcut { * @throws {@link IllegalArgumentException} If target archive view is not supplied */ Collection dependencies(String... coordinates) throws ResolutionException; + + /** + * Resolves dependency for dependency builder. + * + * @param coordinates Coordinates specified to a created artifact, specified in an implementation-specific format. + * @return A File which contain resolved artifact. + * @throws ResolutionException If artifact could not be resolved + */ + File resolveAsFile(String coordinates) throws ResolutionException; + + /** + * Resolves dependencies for dependency builder. + * + * @param coordinates A list of coordinates specified to the created artifacts, specified in an implementation-specific + * format. + * @return An array of Files which contains resolved artifacts + * @throws ResolutionException If artifact could not be resolved + */ + File[] resolveAsFiles(String... coordinates) throws ResolutionException; } diff --git a/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/Maven.java b/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/Maven.java index 9948a84a7..6796f90e2 100644 --- a/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/Maven.java +++ b/api-maven/src/main/java/org/jboss/shrinkwrap/resolver/api/maven/Maven.java @@ -22,6 +22,7 @@ package org.jboss.shrinkwrap.resolver.api.maven; +import java.io.File; import java.util.Collection; import org.jboss.shrinkwrap.api.GenericArchive; @@ -62,6 +63,29 @@ public static Collection dependencies(String... coordinates) thr return DependencyResolvers.use(MavenDependencyShortcut.class).dependencies(coordinates); } + /** + * Resolves dependency for dependency builder. + * + * @param coordinates Coordinates specified to a created artifact, specified in an implementation-specific format. + * @return A File which contain resolved artifact. + * @throws ResolutionException If artifact could not be resolved + */ + public static File resolveAsFile(String coordinates) throws ResolutionException { + return DependencyResolvers.use(MavenDependencyShortcut.class).resolveAsFile(coordinates); + } + + /** + * Resolves dependencies for dependency builder. + * + * @param coordinates A list of coordinates specified to the created artifacts, specified in an implementation-specific + * format. + * @return An array of Files which contains resolved artifacts + * @throws ResolutionException If artifact could not be resolved + */ + public static File[] resolveAsFiles(String... coordinates) throws ResolutionException { + return DependencyResolvers.use(MavenDependencyShortcut.class).resolveAsFiles(coordinates); + } + /** * Loads remote repositories for a POM file. If repositories are defined in the parent of the POM file and there are * accessible via local file system, they are set as well. @@ -77,5 +101,4 @@ public static Collection dependencies(String... coordinates) thr public static EffectivePomMavenDependencyShortcut withPom(String path) { return DependencyResolvers.use(MavenDependencyShortcut.class).withPom(path); } - } diff --git a/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenDependencyShortcutImpl.java b/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenDependencyShortcutImpl.java index c0e179914..a97ae6ea4 100644 --- a/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenDependencyShortcutImpl.java +++ b/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenDependencyShortcutImpl.java @@ -21,7 +21,9 @@ */ package org.jboss.shrinkwrap.resolver.impl.maven; +import java.io.File; import java.util.Collection; + import org.jboss.shrinkwrap.api.GenericArchive; import org.jboss.shrinkwrap.resolver.api.ResolutionException; import org.jboss.shrinkwrap.resolver.api.maven.EffectivePomMavenDependencyShortcut; @@ -53,7 +55,6 @@ public MavenDependencyShortcutImpl() { */ @Override public GenericArchive dependency(String coordinates) throws ResolutionException { - Collection result = delegate.artifact(coordinates).resolveAs(GenericArchive.class, new StrictFilter()); if (result != null && result.size() != 1) { @@ -77,6 +78,36 @@ public Collection dependencies(String... coordinates) throws Res return delegate.artifacts(coordinates).resolveAs(GenericArchive.class, new StrictFilter()); } + /** + * Resolves dependency for dependency builder. + * + * @param coordinates Coordinates specified to a created artifact, specified in an implementation-specific format. + * @return A File which contain resolved artifact. + * @throws ResolutionException If artifact could not be resolved + */ + @Override + public File resolveAsFile(String coordinates) throws ResolutionException { + File[] result = delegate.artifact(coordinates).resolveAsFiles(new StrictFilter()); + + if (result != null && result.length != 1) { + throw new ResolutionException("Only one file should have been resolved. Resolved " + result.length + " files."); + } + return result[0]; + } + + /** + * Resolves dependencies for dependency builder. + * + * @param coordinates A list of coordinates specified to the created artifacts, specified in an implementation-specific + * format. + * @return An array of Files which contains resolved artifacts + * @throws ResolutionException If artifact could not be resolved + */ + @Override + public File[] resolveAsFiles(String... coordinates) throws ResolutionException { + return delegate.artifacts(coordinates).resolveAsFiles(new StrictFilter()); + } + /** * Loads remote repositories for a POM file. If repositories are defined in the parent of the POM file and there are * accessible via local file system, they are set as well. @@ -90,9 +121,9 @@ public Collection dependencies(String... coordinates) throws Res * @return A dependency builder with remote repositories set according to the content of POM file. * @throws ResolutionException If artifact coordinates are wrong or if version cannot be determined. */ + @Override public EffectivePomMavenDependencyShortcut withPom(final String path, String... profiles) throws ResolutionException { this.delegate = delegate.loadEffectivePom(path, profiles).up(); return this; } - } diff --git a/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/DependenciesUnitTestCase.java b/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/DependenciesUnitTestCase.java index 7fd176def..60a0e20e7 100644 --- a/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/DependenciesUnitTestCase.java +++ b/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/DependenciesUnitTestCase.java @@ -47,12 +47,11 @@ public static void setRemoteRepository() { public static void clearRemoteRepository() { System.clearProperty(MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION); System.clearProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION); - } // -------------------------------------------------------------------------------------|| // Tests - // ------------------------------------------------------------------------------|| + // -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------|| /** @@ -112,6 +111,20 @@ public void testShortcutSimpleResolution() throws ResolutionException { war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + /** + * Tests a resolution of an artifact from central + * + * @throws ResolutionException + */ + @Test + public void testShortcutSimpleResolutionAsFile() throws ResolutionException { + File file = Maven.resolveAsFile("org.jboss.shrinkwrap.test:test-deps-c:1.0.0"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-deps-c-shortcut.tree")); + desc.validateFiles(file).results(); + } + /** * Tests a resolution of an artifact from central with custom settings * @@ -130,7 +143,6 @@ public void testSimpleResolutionWithCustomSettings() throws ResolutionException desc.validateArchive(war).results(); war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); - } /** @@ -145,7 +157,6 @@ public void testInvalidSettingsPath() throws ResolutionException { ShrinkWrap.create(WebArchive.class, "testSimpleResolutionWithCustomSettings.war").addAsLibraries( DependencyResolvers.use(MavenDependencyResolver.class).configureFrom("src/test/invalid/custom-settings.xml") .artifact("org.jboss.shrinkwrap.test:test-deps-c:1.0.0").resolveAs(GenericArchive.class)); - } /** @@ -166,7 +177,6 @@ public void testMultipleResolution() throws ResolutionException { desc.validateArchive(war).results(); war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); - } /** @@ -190,7 +200,6 @@ public void testMultipleResolutionSingleCall() throws ResolutionException { desc.validateArchive(war).results(); war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); - } /** @@ -213,4 +222,19 @@ public void testShortcutMultipleResolutionSingleCall() throws ResolutionExceptio war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + + /** + * Tests a resolution of two artifacts from central using single call + * + * @throws ResolutionException + */ + @Test + public void testShortcutMultipleResolutionSingleCallAsFiles() throws ResolutionException { + File[] files = Maven.resolveAsFiles("org.jboss.shrinkwrap.test:test-deps-c:1.0.0", + "org.jboss.shrinkwrap.test:test-deps-g:1.0.0"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-deps-c+g-shortcut.tree")); + desc.validateFiles(files).results(); + } } diff --git a/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/PomDependenciesUnitTestCase.java b/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/PomDependenciesUnitTestCase.java index 91a11ed25..3413c9a7e 100644 --- a/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/PomDependenciesUnitTestCase.java +++ b/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/PomDependenciesUnitTestCase.java @@ -85,6 +85,20 @@ public void testShortcutParentPomRepositories() throws ResolutionException { war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + /** + * Tests loading of a POM file with parent not available on local file system + * + * @throws ResolutionException + */ + @Test + public void testShortcutParentPomRepositoriesAsFile() throws ResolutionException { + File file = Maven.withPom("target/poms/test-child.xml").resolveAsFile("org.jboss.shrinkwrap.test:test-child:1.0.0"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-child-shortcut.tree"), "compile"); + desc.validateFiles(file).results(); + } + /** * Tests loading of a POM file with parent available on local file system * @@ -95,9 +109,8 @@ public void testParentPomRemoteRepositories() throws ResolutionException { String name = "parentPomRemoteRepositories"; WebArchive war = ShrinkWrap.create(WebArchive.class, name + ".war").addAsLibraries( - DependencyResolvers.use(MavenDependencyResolver.class) - .loadEffectivePom("target/poms/test-remote-child.xml").up() - .artifact("org.jboss.shrinkwrap.test:test-deps-c:1.0.0").resolveAs(GenericArchive.class)); + DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("target/poms/test-remote-child.xml") + .up().artifact("org.jboss.shrinkwrap.test:test-deps-c:1.0.0").resolveAs(GenericArchive.class)); DependencyTreeDescription desc = new DependencyTreeDescription(new File( "src/test/resources/dependency-trees/test-deps-c.tree")); @@ -125,6 +138,21 @@ public void testShortcutParentPomRemoteRepositories() throws ResolutionException war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + /** + * Tests loading of a POM file with parent available on local file system + * + * @throws ResolutionException + */ + @Test + public void testShortcutParentPomRemoteRepositoriesAsFile() throws ResolutionException { + File file = Maven.withPom("target/poms/test-remote-child.xml").resolveAsFile( + "org.jboss.shrinkwrap.test:test-deps-c:1.0.0"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-deps-c-shortcut.tree")); + desc.validateFiles(file).results(); + } + /** * Tests loading of a POM file with parent available on local file system Uses POM to get artifact version * @@ -135,9 +163,8 @@ public void testArtifactVersionRetrievalFromPom() throws ResolutionException { String name = "artifactVersionRetrievalFromPom"; WebArchive war = ShrinkWrap.create(WebArchive.class, name + ".war").addAsLibraries( - DependencyResolvers.use(MavenDependencyResolver.class) - .loadEffectivePom("target/poms/test-remote-child.xml").up() - .artifact("org.jboss.shrinkwrap.test:test-deps-c").resolveAs(GenericArchive.class)); + DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("target/poms/test-remote-child.xml") + .up().artifact("org.jboss.shrinkwrap.test:test-deps-c").resolveAs(GenericArchive.class)); DependencyTreeDescription desc = new DependencyTreeDescription(new File( "src/test/resources/dependency-trees/test-deps-c.tree")); @@ -165,6 +192,20 @@ public void testShortcutArtifactVersionRetrievalFromPom() throws ResolutionExcep war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + /** + * Tests loading of a POM file with parent available on local file system Uses POM to get artifact version + * + * @throws ResolutionException + */ + @Test + public void testShortcutArtifactVersionRetrievalFromPomAsFile() throws ResolutionException { + File file = Maven.withPom("target/poms/test-remote-child.xml").resolveAsFile("org.jboss.shrinkwrap.test:test-deps-c"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-deps-c-shortcut.tree")); + desc.validateFiles(file).results(); + } + /** * Tests loading of a POM file with parent available on local file system. However, the artifact version is not used from * there, but specified manually @@ -176,9 +217,8 @@ public void testArtifactVersionRetrievalFromPomOverride() throws ResolutionExcep String name = "artifactVersionRetrievalFromPomOverride"; WebArchive war = ShrinkWrap.create(WebArchive.class, name + ".war").addAsLibraries( - DependencyResolvers.use(MavenDependencyResolver.class) - .loadEffectivePom("target/poms/test-remote-child.xml").up() - .artifact("org.jboss.shrinkwrap.test:test-deps-c:2.0.0").resolveAs(GenericArchive.class)); + DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("target/poms/test-remote-child.xml") + .up().artifact("org.jboss.shrinkwrap.test:test-deps-c:2.0.0").resolveAs(GenericArchive.class)); DependencyTreeDescription desc = new DependencyTreeDescription(new File( "src/test/resources/dependency-trees/test-deps-c-2.tree")); @@ -207,6 +247,22 @@ public void testShortcutArtifactVersionRetrievalFromPomOverride() throws Resolut war.as(ZipExporter.class).exportTo(new File("target/" + name + ".war"), true); } + /** + * Tests loading of a POM file with parent available on local file system. However, the artifact version is not used from + * there, but specified manually + * + * @throws ResolutionException + */ + @Test + public void testShortcutArtifactVersionRetrievalFromPomOverrideAsFile() throws ResolutionException { + File file = Maven.withPom("target/poms/test-remote-child.xml").resolveAsFile( + "org.jboss.shrinkwrap.test:test-deps-c:2.0.0"); + + DependencyTreeDescription desc = new DependencyTreeDescription(new File( + "src/test/resources/dependency-trees/test-deps-c-2-shortcut.tree")); + desc.validateFiles(file).results(); + } + /** * Tests resolution of dependencies for a POM file with parent on local file system * @@ -237,9 +293,8 @@ public void testPomRemoteBasedDependencies() throws ResolutionException { String name = "pomRemoteBasedDependencies"; WebArchive war = ShrinkWrap.create(WebArchive.class, name + ".war").addAsLibraries( - DependencyResolvers.use(MavenDependencyResolver.class) - .loadEffectivePom("target/poms/test-remote-child.xml").importAllDependencies() - .resolveAs(JavaArchive.class)); + DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("target/poms/test-remote-child.xml") + .importAllDependencies().resolveAs(JavaArchive.class)); DependencyTreeDescription desc = new DependencyTreeDescription(new File( "src/test/resources/dependency-trees/test-remote-child.tree"));