------ Testing Complex Mojo Parameters ------ 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 Complex Mojo Parameters <>: This example improves the {{{../getting-started/index.html}cookbook}} for testing complex Mojo parameters. In real plugin development, you will use specific Maven objects like <<>>, <<>> or <<>>. You could use them by defining stubs. Suppose that you have the following dependencies in the maven-my-plugin pom: ----- ... org.apache.maven maven-artifact 2.0.8 org.apache.maven maven-project 2.0.8 ... ----- You will add the following in the <<>>: ----- public class MyMojo extends AbstractMojo { /** * The Maven Project. * * @parameter expression="${project}" * @required * @readonly */ protected MavenProject project; /** * Local Repository. * * @parameter expression="${localRepository}" * @required * @readonly */ protected ArtifactRepository localRepository; /** * The Maven Settings. * * @parameter default-value="${settings}" * @required * @readonly */ private Settings settings; ... } ----- * Create Stubs You need to create stub objects to run <<>>. By convention, the package name should reflect the stubs, i.e. in our case <<>>. ----- public class MyProjectStub extends MavenProjectStub { /** * Default constructor */ public MyProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model; try { model = pomReader.read( ReaderFactory.newXmlReader( new File( getBasedir(), "pom.xml" ) ) ); setModel( model ); } catch ( Exception e ) { throw new RuntimeException( e ); } setGroupId( model.getGroupId() ); setArtifactId( model.getArtifactId() ); setVersion( model.getVersion() ); setName( model.getName() ); setUrl( model.getUrl() ); setPackaging( model.getPackaging() ); Build build = new Build(); build.setFinalName( model.getArtifactId() ); build.setDirectory( getBasedir() + "/target" ); build.setSourceDirectory( getBasedir() + "/src/main/java" ); build.setOutputDirectory( getBasedir() + "/target/classes" ); build.setTestSourceDirectory( getBasedir() + "/src/test/java" ); build.setTestOutputDirectory( getBasedir() + "/target/test-classes" ); setBuild( build ); List compileSourceRoots = new ArrayList(); compileSourceRoots.add( getBasedir() + "/src/main/java" ); setCompileSourceRoots( compileSourceRoots ); List testCompileSourceRoots = new ArrayList(); testCompileSourceRoots.add( getBasedir() + "/src/test/java" ); setTestCompileSourceRoots( testCompileSourceRoots ); } /** {@inheritDoc} */ public File getBasedir() { return new File( super.getBasedir() + "/src/test/resources/unit/project-to-test/" ); } } ----- ----- public class SettingsStub extends Settings { /** {@inheritDoc} */ public List getProxies() { return Collections.EMPTY_LIST; } } ----- * Configure <<>> pom ----- ... maven-my-plugin target/test-harness/project-to-test ${localRepository} -----