Testing Using Repositories
Note: This example improves the cookbook for testing repositories.
When developing a Maven plugin you often need to play with repositories. Suppose that the MyMojo needs to download artifacts into your local repository, i.e.:
public class MyMojo extends AbstractMojo { /** * Used for resolving artifacts */ @Component private ArtifactResolver resolver; /** * Factory for creating artifact objects */ @Component private ArtifactFactory factory; /** * Local Repository. */ @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) private ArtifactRepository localRepository; public void execute() throws MojoExecutionException { ... Artifact artifact = factory.createArtifact( "junit", "junit", "3.8.1", "compile", "jar" ); try { resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository ); } catch ( ArtifactResolutionException e ) { throw new MojoExecutionException( "Unable to resolve artifact:" + artifact, e ); } catch ( ArtifactNotFoundException e ) { throw new MojoExecutionException( "Unable to find artifact:" + artifact, e ); } ... } }
Create Stubs
Stub for the test project:
public class MyProjectStub extends MavenProjectStub { /** * Default constructor */ public MyProjectStub() { ... } /** {@inheritDoc} */ public List getRemoteArtifactRepositories() { ArtifactRepository repository = new DefaultArtifactRepository( "central", "http://repo.maven.apache.org/maven2", new DefaultRepositoryLayout() ); return Collections.singletonList( repository ); } }
Configure project-to-test
pom
<project> ... <build> <plugins> <plugin> <artifactId>maven-my-plugin</artifactId> <configuration> <!-- Specify where this pom will output files --> <outputDirectory>${basedir}/target/test-harness/project-to-test</outputDirectory> <!-- By default <<<${basedir}/target/local-repo", where basedir refers to the basedir of maven-my-plugin. --> <localRepository>${localRepository}</localRepository> <!-- The defined stub --> <project implementation="org.apache.maven.plugin.my.stubs.MyProjectStub"/> </configuration> </plugin> </plugins> </build> </project>
Execute test
Calling mvn test
will create ${basedir}/target/local-repo/junitjunit/3.8.1/junit-3.8.1.jar
file.