139 lines
3.7 KiB
Plaintext
139 lines
3.7 KiB
Plaintext
------
|
|
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
|
|
|
|
<<Note>>: This example improves the {{{../getting-started/index.html}cookbook}} for repositories testing.
|
|
|
|
Developing Maven plugin needs often to have to play with repositories. Suppose that the MyMojo needs
|
|
to download artifacts in 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 );
|
|
}
|
|
}
|
|
-----
|
|
|
|
* Configuring <<<project-to-test>>> pom
|
|
|
|
-----
|
|
<project>
|
|
...
|
|
<build>
|
|
<plugins>
|
|
<plugin>
|
|
<artifactId>maven-my-plugin</artifactId>
|
|
<configuration>
|
|
<!-- Specify where this pom will be outputted files -->
|
|
<outputDirectory>${basedir}/target/test-harness/project-to-test</outputDirectory>
|
|
|
|
<!-- By default <<<${basedir}/target/local-repo", where basedir refer
|
|
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.
|