Groovy-Maven-Plugins
Eine Snippet-Sammlung für Groovy-Maven-Plugins.
Siehe auch:
Hello-World Groovy Maven-Plugins
Details siehe: GMaven Webseite
Hilfreiche Api-Dokus:
- org.apache.maven.artifact
- org.apache.maven.model
- org.apache.maven.project
- org.apache.maven.settings
- Maven Core
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup</groupId>
<artifactId>maven-mypurpose-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.1-SNAPSHOT</version>
<properties>
<!-- Read by gmaven. Workaround for bug: http://jira.codehaus.org/browse/GMAVEN-36 -->
<gmaven.runtime>1.5</gmaven.runtime>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-mojo</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/groovy/MyMojo.groovy
:
import org.codehaus.groovy.maven.mojo.GroovyMojo
/**
* @goal run
*/
public class MyMojo extends GroovyMojo {
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
org.apache.maven.project.MavenProject project
void execute() {
File someDir = new File(project.basedir, "/target/stuff")
ant.copy(todir: someDir) {
fileset(dir: new File(project.basedir, "src/stuff"))
}
}
}
Benutzende pom.xml
:
<project>
...
<build>
<plugins>
<plugin>
<groupId>mygroup</groupId>
<artifactId>maven-mypurpose-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Dependency-Artefakte iterieren
src/main/groovy/MyMojo.groovy
:
/**
* @goal run
* @requiresDependencyResolution compile
*/
public class MyMojo extends org.codehaus.groovy.maven.mojo.GroovyMojo {
/**
* @parameter expression="${project}"
*/
org.apache.maven.project.MavenProject project
void execute() {
project.dependencyArtifacts.each { dep ->
log.info("dep: ${dep.artifactId}, file: ${dep.file.absolutePath}")
}
}
}
Siehe:
- API der Artefakte: org.apache.maven.artifact.Artifact
Nachladen von Artefakten
Im Folgenden wird gezeigt, wie man als Maven-Plugin ein beliebiges Artefakt nachladen kann, auch wenn das Artefakt in keiner POM (z.B. als Dependency) aufgeführt ist.
src/main/groovy/MyMojo.groovy
:
/**
* @goal run
*/
public class MyMojo extends org.codehaus.groovy.maven.mojo.GroovyMojo {
/**
* @component
*/
org.apache.maven.artifact.factory.ArtifactFactory artifactFactory
/**
* @component
*/
org.apache.maven.artifact.resolver.ArtifactResolver artifactResolver
/**
* @parameter expression="${project.remoteArtifactRepositories}"
*/
List remoteRepositories
/**
* @parameter expression="${localRepository}"
*/
org.apache.maven.artifact.repository.ArtifactRepository localRepository
void execute() {
String groupId = "..."
String artifactId = "..."
String version = "..."
String type = "jar"
String classifier = null // "source"
def artifact = artifactFactory.createArtifactWithClassifier(
groupId, artifactId, version, type, classifier)
artifactResolver.resolve(artifact, remoteRepositories, localRepository)
log.info("## path: " + artifact.file.absolutePath)
}
}