Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Maven User
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzppbmRlbnQ9MjBweHxzdHlsZT1kaXNjfQ&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="indent=20px|style=disc"> <h1>Mojo Developer Cookbook</h1> <p> This page contains code snippets demonstrating commonly used tasks when writing Maven plug-ins. There are no guarantees that these are the 'right' way to do things, but they have worked at least once for the author(s).</p> <h2>Commonly used components </h2> <p>These components are commonly used, also in the following snippets.</p> <h3>The maven project, or the effective pom.</h3> <p>With the MavenProject object, you would be able to access a lot of things ( that is if you don't know the @component or @expression for it. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @parameter default-value="${project}" */ private org.apache.maven.project.MavenProject mavenProject; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h3>Creating a maven project</h3> <p><br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @component */ private MavenProjectBuilder mavenProjectBuilder; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h3>The dependencies declared in your plugin.</h3> <p><br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @parameter default-value="${plugin.artifacts}" */ private java.util.List pluginArtifacts; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h3>For accessing artifacts and repositories</h3> <p><br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @component */ private org.apache.maven.artifact.factory.ArtifactFactory artifactFactory; /** @component */ private org.apache.maven.artifact.resolver.ArtifactResolver resolver; /**@parameter default-value="${localRepository}" */ private org.apache.maven.artifact.repository.ArtifactRepository localRepository; /** @parameter default-value="${project.remoteArtifactRepositories}" */ private java.util.List remoteRepositories; /** @parameter default-value="${project.distributionManagementArtifactRepository}" */ private ArtifactRepository deploymentRepository; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h3>For resolving artifact verions</h3> <p><br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @component */ private ArtifactMetadataSource artifactMetadataSource; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h3>Deploying and installing</h3> <p><br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> /** @component */ private ArtifactDeployer deployer; /** @component */ private ArtifactInstaller installer; </pre></td></tr></table> <p><br class="atl-forced-newline" /> You may want to add @required and @readonly, but I dropped them to keep it short. <br class="atl-forced-newline" /> <br class="atl-forced-newline" /> The ones listed here are the common ones. But you can use other properties in default-value="${...}". See <a class="confluence-link" href="/display/MAVENUSER/MavenPropertiesGuide" data-linked-resource-id="48421" data-linked-resource-type="page" data-linked-resource-default-alias="MavenPropertiesGuide" data-base-url="http://docs.codehaus.org">MavenPropertiesGuide</a> for more info...</p> <h2>Dependencies for Components</h2> <p>When developing with the above mentioned commonly used components you will<br /> want to add the following dependencies to your pom.xml: <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> <version>2.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> <version>2.0</version> <scope>compile</scope> </dependency> </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Storing Properties</h2> <p> If you want to store properties in your mojo and make those properties accessible to other elements ( such as plugins, resource files, etc ). You can do something like this <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> mavenProject.getProperties().put( "my.mojo.property", "my mojo value" ) </pre></td></tr></table> <p><br class="atl-forced-newline" /> This is like adding to your pom <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6eG1sfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <project> ... <properties> <my.mojo.property>my mojo value</my.mojo.property> </properties> </project> </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Creating and resolving an artifact</h2> <p> To create an artifact, use an ArtifactFactory. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier); </pre></td></tr></table> <p><br class="atl-forced-newline" /> An artifact doesn't help you much until it's resolved (using an ArtifactResolver). This resolving process is not transitive! <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> resolver.resolve( artifact, remoteRepositories, localRepository ); </pre></td></tr></table> <p><br class="atl-forced-newline" /> Now you get the artifact file (in the local repository): <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> File artifactFile = artifact.getFile(); </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Resolving an artifact from a version range</h2> <p>First you need to create the version range:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> versionRange = VersionRange.createFromVersionSpec(versionSpecification); </pre></td></tr></table> <p>Then you need to create the artifact from the version range:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> artifact = artifactFactory.createDependentArtifact(groupId, artifactId, versionRange, type, classifier, scope); // or artifact = artifactFactory.createExtensionArtifact(groupId, artifactId, versionRange); // or artifact = artifactFactory.createPluginArtifact(groupId, artifactId, versionRange); </pre></td></tr></table> <p>Once you have the artifact with version range you use the ArtifactMetadataSource to retrieve the versions:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> List/*<ArtifactVersion>*/ versions = artifactMetatdataSource.retrieveAvailableVersions(artifact, localRepository, remoteRepository); </pre></td></tr></table> <p>Then you can parse the list of versions and find the version that you are after (for example using versionRange.matchVersion) and then create a new artifact using tha version.</p> <p><br class="atl-forced-newline" /></p> <h2>Resolving transitively</h2> <p>You need a MavenProject. If you don't have (the right) one, you can create it with just a groupId:artifactId:version: <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Artifact pomArtifact = artifactFactory.createArtifact(groupId, artifactId, version, classifier, "pom"); MavenProject pomProject = mavenProjectBuilder.buildFromRepository( pomArtifact, remoteRepositories, localRepositories ); </pre></td></tr></table> <p><br class="atl-forced-newline" /> Now you can use the ArtifactResolver to resolve transitively. <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Set artifacts = pomProject.createArtifacts( this.factory, null, null); ArtifactFilter filter = ... ArtifactResolutionResult arr = resolver.resolveTransitively(artifacts, pomArtifact, pomProject.getManagedVersionMap(), local, remoteRepos, source, filter); Set result = arr.getArtifacts(); </pre></td></tr></table> <p><br class="atl-forced-newline" /> The filter is optional, and limits the resolving. Possible filters are: <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <ul> <li>ScopeArtifactFilter</li> <li>InclusionArtifactFilter</li> <li>ExclusionArtifactFilter</li> <li>TypeArtifactFilter</li> <li>AndArtifactFilter (to combine them)</li> <li>or create your own, by implementing ArtifactFilter</li> </ul> <h2>Running external commands</h2> <p>Use CommandLine and CommandLineUtils from org.codehaus.plexus.util.cli <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> CommandLine cl = new CommandLine("command"); cl.addArguments( new String[] { "arg1", "arg2", "arg3" } ); InputStream input = ... StreamConsumer output = ... StreamConsumer error = ... int returnValue = CommandLineUtils.executeCommandLine(cl, input, output, error); </pre></td></tr></table> <p><br class="atl-forced-newline" /> The input parameter is optional. For the output and error StreamConsumers, you can use CommandLineUtils.StringStreamConsumer which captures the output<br /> in a String. <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Installing and deploying artifacts</h2> <p>If you need want to install or deploy an artifact as part of a project build, you can add them to the MavenProject and they will be picked up in the install or deploy phase. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> project.getArtifact().setFile(file) // for the main artifact projectHelper.attachArtifact(project, type, classifier, file) // for attached artifacts </pre></td></tr></table> <p><br class="atl-forced-newline" /> If you install/deploy in a standalone goal, or you need to install/deploy an artifact that is not part of the project being built, you can use the ArtifactInstaller or ArtifactDeployer. <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier); installer.install(file, artifact, localRepository); // to install deployer.deploy(file, artifact, deploymentRepository, localRepository); // to deploy (includes the install) </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Creating an ArtifactRepository</h2> <p>Usually an artifact repository will be injected into your mojo. Should you need to create one yourself, you will need an ID, a URL and a layout. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ArtifactRepository repo = new DefaultArtifactRepository( "repositoryId", "http://my.repository.url", new DefaultRepositoryLayout() ); // use LegacyRepositoryLayout for an m1 repo&nbsp; </pre></td></tr></table> <p><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <h2>Accessing the Plexus container</h2> <p>If you want access to the container, for doing your own component lookups, implement the org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable interface</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public void contextualize( Context context ) throws ContextException { container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );} </pre></td></tr></table> <p>So if you want to lookup a component, you can do something like this</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Object myComponent = container.lookup( role, roleHint, container.getLookupRealm() ); </pre></td></tr></table> <h2>Getting dependency artifact path</h2> <p>In some cases you may need to get the path of a jar artifact from a Dependency. This is how you can do it: <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Set artifacts = project.getDependencyArtifacts(); for (Iterator artifactIterator = artifacts.iterator(); artifactIterator.hasNext();) { Artifact artifact = (Artifact) artifactIterator.next(); if (artifact.getGroupId().equals(dependency.getGroupId()) && artifact.getArtifactId().equals(dependency.getArtifactId())) { return artifact.getFile().getPath(); } } </pre></td></tr></table> <p><br class="atl-forced-newline" /> In this snippet, project is the <a href="http://maven.apache.org/ref/2.0.4/maven-project/apidocs/org/apache/maven/project/MavenProject.html">MavenProject</a> instance of the project (@parameter default-value="${project}") and dependency is instance of Dependency (@parameter default-value="${project.dependencies}). <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced