Maven Plugins
-
Upload
evgeny-goldin -
Category
Technology
-
view
5.028 -
download
3
description
Transcript of Maven Plugins
Maven Plugins
Evgeny Goldin
evgeny-goldin.com/wiki/Maven-pluginsevgeny-goldin.org/artifactory/plugins
-releasesevgeny-goldin.org/artifactory/libs
-releases
<groupId>com.goldin.plugins</groupId><artifactId>maven-xyz-plugin</
artifactId><version>0.2.2</version>
maven-copy-plugin– Copies, packs, unpacks, downloads,
uploads ..– .. files, archives, Maven
<dependencies>
maven-properties-plugin– Creates new Maven properties at run-
time ..– .. evaluating Groovy expressions!
maven-jenkins-plugin– Generates Hudson/Jenkins jobs– Organizes them hierarchically in a single
POM
maven-assert-plugin– Verifies files exist, properties defined,
etc.– Verifies any Groovy expression!
maven-mail-plugin– Sends e-mails with attachments from
Maven–Works great for build-time reports
maven-spring-batch-plugin– Invokes Spring Batch job as part of
Maven build
maven-copy-plugin
• Copies, packs, unpacks, downloads, uploads ..
• .. files, archives, Maven <dependencies>• Filters and replaces content• Updates archives• Unpacks Zip entries• Downloads and uploads from/to HTTP,
SCP, FTP• Built-in Groovy support
<groupId>com.goldin.plugins</groupId><artifactId>maven-copy-plugin</
artifactId><version>0.2.2</version>
<configuration>:
<configuration> <resources> <resource>..</resource> <resource>..</resource> </resources><configuration>
Copy files:
<resource> <targetPath>target
directory</targetPath> <directory>base directory</directory> <include>**/*.xml, *.txt,
**/lib/*.jar</include> <exclude>**/*-template.xml</exclude></resource>
Copy file:
<resource> <targetPath>target
directory</targetPath> <file>file to copy</file></resource>
Filter files:
<resource> <targetPath>..</targetPath> <directory>..</directory> <include>..</include> <filtering>true</filtering></resource>
Replace content – single <replace>:
<resource> <targetPath>..</targetPath> <file>..</file> <replace> <from>regular expression</from> <to>replacement content</to> </replace></resource>
Replace content - multiple <replaces>:
<resource> <targetPath>..</targetPath> <file>..</file> <replaces> <replace>..</replace> <replace>..</replace> </replaces></resource>
<filter> + <replace>:
<resource> <targetPath>..</targetPath> <file>..</file> <filtering>true</filtering> <replace>..</replaces></resource>
Copy <dependency>:
<resource> <targetPath>..</targetPath> <dependency> <groupId>..</groupId> <artifactId>..</artifactId> </dependency></resource>
Copy <dependencies>:
<resource> <targetPath>..</targetPath> <dependencies> <dependency>..</dependency> <dependency>..</dependency> </dependencies></resource>
Pack archive:
<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack></resource>
Formats supported:
• Zip, jar, war, ear, hpi• Tar, tar.gz, tgz• Tar.bz2, tzp, tbz2• Ant + TrueZip
Update archive:
<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack> <update>true</update></resource>
Attach artifact:
<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack>
<attachArtifact>true</attachArtifact>
</resource>
Deploy artifact:
<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack> <deploy>repoUrl|groupId|artifactId|
version</deploy>
</resource>
Unpack archive:
<resource> <targetPath>..</targetPath> <file>some/file.zip</directory> <unpack>true</unpack></resource>
Formats supported:
• Zip, jar, war, ear, hpi• Tar, tar.gz, tgz• Tar.bz2, tzp, tbz2• Ant + TrueZip
Unpack archives:
<resource> <targetPath>..</targetPath> <directory>..</directory> <include>*.jar, *.zip, *.tar.gz</include> <unpack>true</unpack></resource>
Unpack <dependency>:
<resource> <targetPath>..</targetPath> <dependency> <groupId>..</groupId> <artifactId>..</artifactId> </dependency> <unpack>true</unpack></resource>
Unpack <dependencies>:
<resource> <targetPath>..</targetPath> <dependencies> <dependency>..</dependency> <dependency>..</dependency> </dependencies> <unpack>true</unpack></resource>
Unpack Zip entry:
<resource> <targetPath>..</targetPath> <file>path/file.zip</file>
<zipEntry>entry/path/file.txt</zipEntry>
<unpack>true</unpack></resource>
Unpack Zip entries (1):
<resource> <targetPath>..</targetPath> <file>path/file.zip</file> <zipEntry>entry/**/*.txt</zipEntry> <unpack>true</unpack></resource>
Unpack Zip entries (2):
<resource> <zipEntries> <zipEntry>..</zipEntry> <zipEntry>..</zipEntry> </zipEntries> <unpack>true</unpack></resource>
Download files – network drive:
<resource> <targetPath>..</targetPath>
<directory>\\network\drive</directory>
<include>..</include> <exclude>..</exclude></resource>
Download file – HTTP:
<resource> <targetPath>..</targetPath> <file>http://host/file</file></resource>
Download file – SCP:
<resource> <targetPath>..</targetPath>
<directory>scp://user:pass@host:/file</directory>
</resource>
Download files – FTP:
<resource> <targetPath>..</targetPath>
<directory>ftp://user:pass@host:/path</directory>
<include>..</include> <exclude>..</exclude></resource>
Download files – FTP + wget:
<resource> <targetPath>..</targetPath>
<directory>ftp://user:pass@host:/path</directory>
<include>..</include> <wget>wget.exe</wget></resource>
Download + unpack:
<resource> <targetPath>..</targetPath> http / scp / ftp <unpack>true</unpack></resource>
Upload files – network drive:
<resource>
<targetPath>\\network\drive</targetPath>
<directory>..</directory> <include>..</include></resource>
Upload files – SCP:
<resource>
<targetPath>scp://user:pass@host:/dir</targetPath>
<directory>..</directory> <include>..</include></resource>
Upload files – FTP:
<resource>
<targetPath>ftp://user:pass@host:/dir</targetPath>
<directory>..</directory> <include>..</include></resource>
Delete files:
<resource> <directory>..</directory> <include>..</include> <clean>true</clean>
<cleanEmptyDirectories>true</cleanEmptyDirectories>
</resource>
Create file (= copy other file + <replace>):
<resource> <targetPath>..</targetPath> <file>${project.basedir}/pom.xml</file> <replace><to>new
content</to></replace>
<destFileName>newName.txt</destFileName>
</resource>
Create directory:
<resource> <targetPath>..</targetPath> <mkdir>true</mkdir></resource>
Groovy support - <runIf>:
<configuration> <runIf>{{ Groovy Expression
}}</runIf> <resource> <runIf>{{ Groovy Expression
}}</runIf> //
Boolean.valueOf( String.valueOf( returnValue ))
</resource></configuration>
Groovy support - <description>:
<resource> <description>Time: {{ new Date()
}}</description> … </resource>
• Logs <resource> start, end, and execution time in ms.
Groovy support - <replace>:
<resource> … <replace> <to>File updated at: {{ new Date()
}}</to> <groovy>true</groovy> </replace>
</resource>
Groovy support – FTP download + <listFilter>:
<resource> FTP download <wget>wget.exe</wget>
<listFilter>{{ files.keySet().findAll{ .. } }}</listFilter>
</resource>
• Filters files to download.
Groovy support - <filter>:
<resource> <targetPath>..</targetPath> … <filter>{{ files.findAll{ .. } }}</filter></resource>
• Filters files to copy, pack, unpack, etc.
Groovy support - <process>:
<resource> <targetPath>..</targetPath> … <process>{{ println “Files: $files” }}</process>
</resource>
• Processes files after they are copied, packed, etc.
Groovy support - context:
• “project” - org.apache.maven.project.MavenProject
• “session” - org.apache.maven.execution.MavenSession
• “mavenVersion”• All Maven and System properties– <this-property> => “thisProperty”– “os.name” => “osName”
• {{ new File( project.basedir, “pom.xml” ) }}
maven-properties-plugin
• Creates new Maven properties at run time ..• .. evaluating Groovy expressions!• Works best in combination with <runIf>• Allows creating dynamic content for the mail
plugin
<groupId>com.goldin.plugins</groupId><artifactId>maven-properties-plugin</
artifactId><version>0.2.2</version>
<configuration>:
<configuration> <properties> <property>..</property> <property>..</property> </properties></configuration>
<property>:
<property> <name>propertyTrue</name> <value>{{ 't' + 'r' + 'u' + 'e' }}</value></property>…<runIf>{{ propertyTrue }}</runIf><runIf>{{ !
Boolean.valueOf( propertyTrue ) }}</runIf>
Groovy context:
• “project” - org.apache.maven.project.MavenProject
• “session” - org.apache.maven.execution.MavenSession
• “mavenVersion”• All Maven and System properties– <this-property> => “thisProperty”– “os.name” => “osName”
• {{ project.basedir.canonicalPath }}
maven-jenkins-plugin
• Generates Hudson/Jenkins jobs• Defines them in a single POM• Jobs can form hierarchicall groups (reuse!)• Supports jobs invocation• Supports Artifactory deployment
<groupId>com.goldin.plugins</groupId><artifactId>maven-jenkins-plugin</
artifactId><version>0.2.2</version>
<configuration> :
<configuration>
<outputDirectory>.jenkins/jobs</outputDirectory>
<jobs> <job>..</job> <job>..</job> </jobs></configuration>
<job> :
<job> <id>jobName</id>
<mavenName>apache-maven-2.2.1</mavenName> <mavenOpts>-Xmx256m</mavenOpts>
<mavenGoals>–e –B –U clean install</mavenGoals>
…</job>
<job> :
• <jdkName>• <pom>• <repositories>• <privateRepository>• <mail>• …• All standard job configuration options.
Extending <job> (1):
<job> <id>baseJob</id> <abstract>true</abstract> <jdkName>..</jdkName> <mavenName>..</mavenName> <mavenGoals>..</mavenGoals></job>
Extending <job> (2):
<job> <id>jobName</id> <parent>baseJob</parent> <repository>..</repository></job>
• Only job-specific values are specified.
Description Table:
Invoking jobs:
<job> <id>jobName</id>
… <invoke> <jobs>jobA, jobB, jobC</jobs> </invoke></job>
Invoking jobs - conditions:
<invoke> <jobs>jobA, jobB, jobC</jobs> <always>false</always> <stable>true</stable> <unstable>false</unstable> <failed>false</failed> </invoke>
Artifactory deployment:
<job> <artifactory> <name>http://host/artifactory</name> <user>deployer</user>
<scrambledPassword>..</scrambledPassword>
</artifactory></job>
Environments supported:
• SCM: Subversion, Git, CVS, NullSCM (none)• Maven jobs• Free-style jobs• Plugins required:– “Parameterized Trigger Plugin” v2.4 or higher– “Artifactory Plugin” v1.3.4 or higher
maven-assert-plugin
• Build assertions!• Properties defined• Files exist• Directories identical• Groovy expressions evaluate to true– Was driven by plugins tests
<groupId>com.goldin.plugins</groupId><artifactId>maven-assert-plugin</
artifactId><version>0.2.2</version>
<configuration> :
<configuration> <assertProperties>..</assertProperties> <assertFiles>..</assertFiles> <assertEqual>..</assertEqual> <assertGroovy>..</assertGroovy></configuration>
<assertProperties> :
<assertProperties> job-parameter BUILD_NUMBER JENKINS_URL</assertProperties>
• Makes sure all job parameters are specified.
<assertFiles> :
<assertFiles> ${data-files}/*.xml ${project.build.directory}/setup.tar.gz ${project.build.directory}/ini</assertFiles>
• Makes sure all files and directories are created.
<assertEqual> :
<assertEqual> ${dir}/expected|${outputDir}/result
${dir}/expected|${outputDir}/result|**/*.xml
</assertEqual>
• Makes sure directories are identical (+/- pattern).
<assertGroovy> :
<assertGroovy> project.basedir.directorySize() > 0 timestamp ==~ /^\d{2} \w+ \d{4}, \d{2}:\
d{2}:\d{2}$/</assertGroovy>
• Evaluates «assert line» for each line (power assert!)
Groovy context:
• File.directorySize()• Object.splitWith() – see GCommons • “project” -
org.apache.maven.project.MavenProject• “session” -
org.apache.maven.execution.MavenSession• “mavenVersion”• All Maven and System properties
maven-mail-plugin
• Sends e-mails with attachments from Maven
• Works great for build-time reports • Properties plugin allows for dynamic
content
<groupId>com.goldin.plugins</groupId><artifactId>maven-mail-plugin</
artifactId><version>0.2.2</version>
<configuration>:
<configuration> <smtp>..</smtp> <from>..</from> <mails><to>..</to></mails> <subject>..</subject> <text>..</text></configuration>
<mails>:
<mails> <to>user@mail; user2@mail</to> <cc>User Name <user@mail>;</cc> <bcc>..</bcc></mails>• Use <user@mail> or CDATA• Addresses are «;»-separated to allow spaces
in names
Attaching files:
<configuration> <textFile>..</textFile> <files> <file>..</file> <file>..</file> </files></configuration>
maven-spring-batch-plugin
• Invokes SpringBatch job as part of Maven build
• Maven is a good platform for batch processes!
<groupId>com.goldin.plugins</groupId><artifactId>maven-spring-batch-
plugin</artifactId><version>0.2.2</version>
<configuration>:
<configuration> <jobId>jobId</jobId> <configLocations> classpath:/springbatch/infra/job-
launcher.xml file:/job/context.xml </configLocations></configuration>
Properties:
<configuration> <props> name = value classpath:/springbatch/infra/job-
runner.properties file:/job/context.properties </props></configuration>
• evgeny-goldin.com/wiki/Maven-plugins• maven-plugins.994461.n3.nabble.com• evgeny-goldin.org/artifactory/plugins
-releases• evgeny-goldin.org/artifactory/libs
-releases• evgeny-goldin.org/youtrack/issues/pl• @evgeny_goldin• [email protected]