------ Testing Using Repositories ------ Vincent Siveton ------ February 2008 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. Testing Using Repositories <>: This example improves the {{{../getting-started/index.html}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 expression="${localRepository}" * @required * @readonly */ 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://repo1.maven.org/maven2", new DefaultRepositoryLayout() ); return Collections.singletonList( repository ); } } ----- * Configure <<>> pom ----- ... maven-my-plugin ${basedir}/target/test-harness/project-to-test ${localRepository} ----- ** Execute test Calling <<>> will create <<<$\{basedir\}/target/local-repo/junitjunit/3.8.1/junit-3.8.1.jar>>> file.