Skip to content

Instantly share code, notes, and snippets.

@kpiwko
Created April 26, 2012 20:29
Show Gist options
  • Save kpiwko/2502823 to your computer and use it in GitHub Desktop.
Save kpiwko/2502823 to your computer and use it in GitHub Desktop.
ShrinkWrap Maven Resolver Use Cases
package org.jboss.shrinkwrap.resolver.test;
import java.io.File;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.MavenConfigurationTypes;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyShortcut;
import org.jboss.shrinkwrap.resolver.api.maven.MavenImporter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.CombinedFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.DependenciesFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ExclusionFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ExclusionsFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ScopeFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.StrictFilter;
/**
* Manifestation of various use cases for ShrinkWrap Maven Resolver
*
* @author <a href="mailto:[email protected]">Karel Piwko</a>
*
*/
public class ShrinkResUsageManifest {
/**
* Use case 1:
*
* Resolve a single artifact without transitive dependencies as Archive<?>
*/
public void singleArtifact() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").resolveAs(Archive.class, new StrictFilter())
.iterator().next();
// or, if GenericArchive is expected, following shortcut can be used
DependencyResolvers.use(MavenDependencyShortcut.class).dependency("G:A:V");
// or even shorter
Maven.dependency("G:A:V");
}
/**
* Use case 2:
*
* Resolve a single artifact without transitive dependencies as File
*/
public void singleArtifactAsFile() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").resolveAsFiles(new StrictFilter());
// or
DependencyResolvers.use(MavenDependencyShortcut.class).resolveAsFile("G:A:V");
// or
Maven.resolveAsFile("G:A:V");
}
/**
* Use case 3:
*
* Resolve a single artifact without transitive dependencies, using version from a POM file
*/
public void singleArtifactWithPomFile() {
// !assignment error!
File f = DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml").artifact("G:A")
.resolveAsFiles(new StrictFilter())[0];
// or
f = DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml").artifact("G:A").exclusion("*")
.resolveAsFiles()[0];
// or
f = DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml")
.importAnyDependencies(new DependenciesFilter("G:A")).resolveAsFiles(new StrictFilter())[0];
// or
f = DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml")
.importAnyDependencies(new DependenciesFilter("G:A")).resolveAsFiles(new DependenciesFilter("G:A"))[0];
// or
DependencyResolvers.use(MavenDependencyShortcut.class).withPom("pom.xml").resolveAsFile("G:A");
// or
Maven.withPom("pom.xml").resolveAsFile("G:A");
// or using ShrinkWrap Maven plugin and current Maven execution
f = DependencyResolvers.use(MavenDependencyResolver.class).configureFrom(MavenConfigurationTypes.ENVIRONMENT)
.artifact("G:A").resolveAsFiles(new StrictFilter())[0];
}
/**
* Use case 4:
*
* Resolve two or more artifacts without transitive dependencies
*/
public void multipleArtifacts() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").artifact("G:B:V")
.resolveAsFiles(new StrictFilter());
// or
DependencyResolvers.use(MavenDependencyResolver.class).artifacts("G:A:V", "G:B:V").resolveAsFiles(new StrictFilter());
// or
DependencyResolvers.use(MavenDependencyShortcut.class).resolveAsFiles("G:A:V", "G:B:V");
// or
Maven.resolveAsFiles("G:A:V", "G:B:V");
}
/**
* Use case 5:
*
* Resolve an artifact with transitive dependencies
*/
public void transitiveArtifact() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").resolveAsFiles();
}
/**
* Use case 6:
*
* Resolve an artifact with transitive dependencies using extra exclusion
*/
public void transitiveArtifactExtraExclusion() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").exclusion("G:B").resolveAsFiles();
// or
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").resolveAsFiles(new ExclusionFilter("G:B"));
}
/**
* Use case 7:
*
* Resolve artifacts with transitive dependencies using extra exclusions
*/
public void transitiveArtifactsExtraExclusions() {
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").exclusion("G:B").artifact("G:B:V")
.exclusion("G:C").resolveAsFiles();
// or
DependencyResolvers.use(MavenDependencyResolver.class).artifact("G:A:V").artifact("G:B:V")
.resolveAsFiles(new ExclusionsFilter("G:B", "G:C"));
// or
// note, this does exclusion of both exclusions for both artifacts which is not same!
DependencyResolvers.use(MavenDependencyResolver.class).artifacts("G:A:V", "G:B:V").exclusions("G:B", "G:C")
.resolveAsFiles();
}
/**
* Use case 8:
*
* Resolve an artifact with transitive dependencies, using pom for version
*/
public void transitiveArtifactWithPom() {
DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml").artifact("G:A").resolveAsFiles();
// or using ShrinkWrap Maven plugin and current Maven execution
DependencyResolvers.use(MavenDependencyResolver.class).configureFrom(MavenConfigurationTypes.ENVIRONMENT)
.artifact("G:A").resolveAsFiles();
}
/**
* Use case 9:
*
* Import the same dependencies as Maven would do.
*/
public void mimickMavenDependencies() {
DependencyResolvers.use(MavenDependencyResolver.class).loadSettings("settings.xml").loadEffectivePom("pom.xml")
.importAnyDependencies(new ScopeFilter("compile", "runtime", "")).resolveAsFiles();
// or using ShrinkWrap Maven plugin and current Maven execution
DependencyResolvers.use(MavenDependencyResolver.class).configureFrom(MavenConfigurationTypes.ENVIRONMENT)
.importAnyDependencies(new ScopeFilter("compile", "runtime", "")).resolveAsFiles();
// or using MavenImporter, which does a bit different thing
ShrinkWrap.create(MavenImporter.class).loadSettings("settings.xml").loadEffectivePom("pom.xml")
.importAnyDependencies(new ScopeFilter("compile", "runtime", ""));
}
/**
* Use case 10:
*
* Import test dependencies and exclude G:A:V
*/
public void importTestDependenciesWithExtraExclusion() {
DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml")
.importTestDependencies(new ExclusionFilter("G:A")).resolveAsFiles();
// or
DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml").importTestDependencies()
.resolveAsFiles(new ExclusionFilter("G:A:V"));
// or
// note this would not work if G:A:V is a transitive dependency!
DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml")
.importAnyDependencies(new CombinedFilter(new ScopeFilter("test"), new ExclusionFilter("G:A:V")))
.resolveAsFiles();
}
/**
* Use case 11:
*
* Import test dependencies and exclude arquillian/shrinkwrap/container (SHRINKRES-30)
*/
public void importTestDependenciesWithArquillianExclusions() {
// solution 1 = enumerate within previous use case
// solution 2 = write a GroupExclusionFilter, note that MavenDependency has no getter for groupId!
// solution 3 = move shrinkwrap/arquillian/container to a distinct profile, then exclude it
}
/**
* Use case 12:
*
* Import a dependency using different classloader (SHRINKRES-26)
*/
public void bootstrapShrinResWithDifferentClassloader() {
// not possible
}
/**
* Use case 13:
*
* Do the same as Maven would do
*/
public void mimickMaven() {
ShrinkWrap
.create(WebArchive.class)
.addClasses(Class.class)
.addAsResource("resources")
.addAsLibraries(
DependencyResolvers.use(MavenDependencyResolver.class).loadEffectivePom("pom.xml")
.importAnyDependencies(new ScopeFilter("compile", "", "runtime")).resolveAsFiles());
// or
// note current implementation is expecting mvn package to be run first (SHRINKRES-18)
ShrinkWrap.create(MavenImporter.class).loadEffectivePom("pom.xml").importBuildOutput();
// note usage of ENVIRONMENT configuration is not possible
}
}
@mojavelinux
Copy link

Cool!

Yep, you have to add a comment for the notification to kick in.

@ALRubinger
Copy link

Linking this back to the forum Thread from which this discussion is based: https://community.jboss.org/message/732721

@88mary256
Copy link

I can't use DependencyResolvers, Do I need any additional thing in my POM file?

@88mary256
Copy link

This is my POM file:


4.0.0

<groupId>ch.swissbytes.mitsuba.giftcard</groupId>
<artifactId>mitsuba-giftcard-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mitsuba-giftcard-web</name>
<description>A starter Java EE 6 webapp project for use on JBoss AS 7 / EAP 6, generated from the jboss-javaee6-webapp archetype</description>

<url>http://jboss.org/jbossas</url>
<licenses>
    <license>
        <name>Apache License, Version 2.0</name>
        <distribution>repo</distribution>
        <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
    </license>
</licenses>

<properties>
    <!-- Explicitly declaring the source encoding eliminates the following 
        message: -->
    <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered 
        resources, i.e. build is platform dependent! -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- JBoss dependency versions -->
    <version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>

    <!-- Define the version of the JBoss BOMs we want to import to specify 
        tested stacks. -->
    <version.jboss.bom>1.0.7.Final</version.jboss.bom>
    <!-- Alternatively, comment out the above line, and un-comment the line 
        below to use version 1.0.4.Final-redhat-4 which is a release certified to 
        work with JBoss EAP 6. It requires you have access to the JBoss EAP 6 maven 
        repository. -->
    <!-- <version.jboss.bom>1.0.4.Final-redhat-4</version.jboss.bom>> -->

    <!-- other plugin versions -->
    <version.surefire.plugin>2.10</version.surefire.plugin>
    <version.war.plugin>2.1.1</version.war.plugin>

    <!-- maven-compiler-plugin -->
    <maven.compiler.target>1.6</maven.compiler.target>
    <maven.compiler.source>1.6</maven.compiler.source>

    <version.arquillian_core>1.0.3.Final</version.arquillian_core>
</properties>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>Prime Repo</name>
        <url>http://repository.primefaces.org</url>
    </repository>
</repositories>


<dependencyManagement>
    <dependencies>
        <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill 
            of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) 
            of artifacts. We use this here so that we always get the correct versions 
            of artifacts. Here we use the jboss-javaee-6.0-with-tools stack (you can 
            read this as the JBoss stack of the Java EE 6 APIs, with some extras tools 
            for your project, such as Arquillian for testing) and the jboss-javaee-6.0-with-hibernate 
            stack you can read this as the JBoss stack of the Java EE 6 APIs, with extras 
            from the Hibernate family of projects) -->
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-tools</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-bom</artifactId>
        <version>2.0.0-beta-2</version>
        <type>pom</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>${arquillian.version}</version>
        <type>pom</type>
        <scope>test</scope>
    </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- querydsl -->

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>3.2.4</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>3.2.4</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>

    <!-- primefaces -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>4.0</version>
    </dependency>
    <!-- Paginator -->
    <dependency>
        <groupId>com.github.timoteoponce</groupId>
        <artifactId>paginator</artifactId>
        <version>0.0.5</version>
    </dependency>
    <!-- First declare the APIs we depend on and need for compilation. All 
        of them are provided by JBoss AS 7 -->

    <!-- Import the CDI API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the Common Annotations API (JSR-250), we use provided scope 
        as the API is included in JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the JAX-RS API, we use provided scope as the API is included 
        in JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.ws.rs</groupId>
        <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the JPA API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the EJB API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSR-303 (Bean Validation) Implementation -->
    <!-- Provides portable constraints such as @Email -->
    <!-- Hibernate Validator is shipped in JBoss AS 7 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Import the JSF API, we use provided scope as the API is included in 
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.faces</groupId>
        <artifactId>jboss-jsf-api_2.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Now we declare any tools needed -->

    <!-- Annotation processor that raising compilation errors whenever constraint 
        annotations are incorrectly used. -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Needed for running tests (you may also use TestNG) -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <!-- <version>4.11</version> -->
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
        <!-- version>2.0.0</version -->
        <!-- scope>test</scope -->
    </dependency>

    <!-- Optional, but highly recommended -->
    <!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA) 
        JPA from JUnit/TestNG -->
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <!-- <version>${arquillian.version}</version>  -->
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.arquillian.protocol</groupId>
        <artifactId>arquillian-protocol-servlet</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- For serializing entities with bidirection navigation -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
        <scope>provided</scope>
    </dependency>


</dependencies>

<build>
    <!-- Maven will append the version to the finalName (which is the name 
        given to the generated war, and hence the context root) -->
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
                <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <!-- The JBoss AS plugin deploys your war to a local JBoss AS container -->
        <!-- To use, run: mvn package jboss-as:deploy -->
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>${version.jboss.maven.plugin}</version>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>

                    <configuration>
                        <sources>
                            <source>src/generated/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- query dsl -->

        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <phase>prepare-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>src/generated/java</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>3.2.4</version>
                </dependency>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-jpa</artifactId>
                    <classifier>apt</classifier>
                    <version>3.2.4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <!-- The default profile skips all tests, though you can tune it to run 
            just unit tests based on a custom pattern -->
        <!-- Seperate profiles are provided for running all tests, including Arquillian 
            tests that execute in the specified container -->
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${version.surefire.plugin}</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <!-- An optional Arquillian testing profile that executes tests in your 
            JBoss AS instance -->
        <!-- This profile will start a new JBoss AS instance, and execute the 
            test, shutting it down when done -->
        <!-- Run with: mvn clean test -Parq-jbossas-managed -->
        <id>arq-jbossas-managed</id>
        <dependencies>
            <dependency>
                <groupId>org.jboss.as</groupId>
                <artifactId>jboss-as-arquillian-container-managed</artifactId>
                <version>7.1.1.Final</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <!-- An optional Arquillian testing profile that executes tests in a remote 
            JBoss AS instance -->
        <!-- Run with: mvn clean test -Parq-jbossas-remote -->
        <id>arq-jbossas-remote</id>
        <dependencies>
            <dependency>
                <groupId>org.jboss.as</groupId>
                <artifactId>jboss-as-arquillian-container-remote</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be used when 
            invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization your app 
            will need. -->
        <!-- By default that is to put the resulting archive into the 'deployments' 
            folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>openshift</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${version.war.plugin}</version>
                    <configuration>
                        <outputDirectory>deployments</outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

</profiles>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment