maven plugins

88
Maven Plugins Evgeny Goldin

Upload: evgeny-goldin

Post on 12-May-2015

5.041 views

Category:

Technology


3 download

DESCRIPTION

Overview of Evgeny Goldin Maven plugins, available at http://evgeny-goldin.com/wiki/Main_Page

TRANSCRIPT

Page 1: Maven Plugins

Maven Plugins

Evgeny Goldin

Page 3: Maven Plugins

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

Page 4: Maven Plugins

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

Page 6: Maven Plugins

• 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

Page 7: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-copy-plugin</

artifactId><version>0.2.2</version>

Page 8: Maven Plugins

<configuration>:

<configuration> <resources> <resource>..</resource> <resource>..</resource> </resources><configuration>

Page 9: Maven Plugins

Copy files:

<resource> <targetPath>target

directory</targetPath> <directory>base directory</directory> <include>**/*.xml, *.txt,

**/lib/*.jar</include> <exclude>**/*-template.xml</exclude></resource>

Page 10: Maven Plugins

Copy file:

<resource> <targetPath>target

directory</targetPath> <file>file to copy</file></resource>

Page 11: Maven Plugins

Filter files:

<resource> <targetPath>..</targetPath> <directory>..</directory> <include>..</include> <filtering>true</filtering></resource>

Page 12: Maven Plugins

Replace content – single <replace>:

<resource> <targetPath>..</targetPath> <file>..</file> <replace> <from>regular expression</from> <to>replacement content</to> </replace></resource>

Page 13: Maven Plugins

Replace content - multiple <replaces>:

<resource> <targetPath>..</targetPath> <file>..</file> <replaces> <replace>..</replace> <replace>..</replace> </replaces></resource>

Page 14: Maven Plugins

<filter> + <replace>:

<resource> <targetPath>..</targetPath> <file>..</file> <filtering>true</filtering> <replace>..</replaces></resource>

Page 15: Maven Plugins

Copy <dependency>:

<resource> <targetPath>..</targetPath> <dependency> <groupId>..</groupId> <artifactId>..</artifactId> </dependency></resource>

Page 16: Maven Plugins

Copy <dependencies>:

<resource> <targetPath>..</targetPath> <dependencies> <dependency>..</dependency> <dependency>..</dependency> </dependencies></resource>

Page 17: Maven Plugins

Pack archive:

<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack></resource>

Page 18: Maven Plugins

Formats supported:

• Zip, jar, war, ear, hpi• Tar, tar.gz, tgz• Tar.bz2, tzp, tbz2• Ant + TrueZip

Page 19: Maven Plugins

Update archive:

<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack> <update>true</update></resource>

Page 20: Maven Plugins

Attach artifact:

<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack>

<attachArtifact>true</attachArtifact>

</resource>

Page 21: Maven Plugins

Deploy artifact:

<resource> <targetPath>path/file.zip</targetPath> <directory>..</directory> <include>..</include> <pack>true</pack> <deploy>repoUrl|groupId|artifactId|

version</deploy>

</resource>

Page 22: Maven Plugins

Unpack archive:

<resource> <targetPath>..</targetPath> <file>some/file.zip</directory> <unpack>true</unpack></resource>

Page 23: Maven Plugins

Formats supported:

• Zip, jar, war, ear, hpi• Tar, tar.gz, tgz• Tar.bz2, tzp, tbz2• Ant + TrueZip

Page 24: Maven Plugins

Unpack archives:

<resource> <targetPath>..</targetPath> <directory>..</directory> <include>*.jar, *.zip, *.tar.gz</include> <unpack>true</unpack></resource>

Page 25: Maven Plugins

Unpack <dependency>:

<resource> <targetPath>..</targetPath> <dependency> <groupId>..</groupId> <artifactId>..</artifactId> </dependency> <unpack>true</unpack></resource>

Page 26: Maven Plugins

Unpack <dependencies>:

<resource> <targetPath>..</targetPath> <dependencies> <dependency>..</dependency> <dependency>..</dependency> </dependencies> <unpack>true</unpack></resource>

Page 27: Maven Plugins

Unpack Zip entry:

<resource> <targetPath>..</targetPath> <file>path/file.zip</file>

<zipEntry>entry/path/file.txt</zipEntry>

<unpack>true</unpack></resource>

Page 28: Maven Plugins

Unpack Zip entries (1):

<resource> <targetPath>..</targetPath> <file>path/file.zip</file> <zipEntry>entry/**/*.txt</zipEntry> <unpack>true</unpack></resource>

Page 29: Maven Plugins

Unpack Zip entries (2):

<resource> <zipEntries> <zipEntry>..</zipEntry> <zipEntry>..</zipEntry> </zipEntries> <unpack>true</unpack></resource>

Page 30: Maven Plugins

Download files – network drive:

<resource> <targetPath>..</targetPath>

<directory>\\network\drive</directory>

<include>..</include> <exclude>..</exclude></resource>

Page 31: Maven Plugins

Download file – HTTP:

<resource> <targetPath>..</targetPath> <file>http://host/file</file></resource>

Page 32: Maven Plugins

Download file – SCP:

<resource> <targetPath>..</targetPath>

<directory>scp://user:pass@host:/file</directory>

</resource>

Page 33: Maven Plugins

Download files – FTP:

<resource> <targetPath>..</targetPath>

<directory>ftp://user:pass@host:/path</directory>

<include>..</include> <exclude>..</exclude></resource>

Page 34: Maven Plugins

Download files – FTP + wget:

<resource> <targetPath>..</targetPath>

<directory>ftp://user:pass@host:/path</directory>

<include>..</include> <wget>wget.exe</wget></resource>

Page 35: Maven Plugins

Download + unpack:

<resource> <targetPath>..</targetPath> http / scp / ftp <unpack>true</unpack></resource>

Page 36: Maven Plugins

Upload files – network drive:

<resource>

<targetPath>\\network\drive</targetPath>

<directory>..</directory> <include>..</include></resource>

Page 37: Maven Plugins

Upload files – SCP:

<resource>

<targetPath>scp://user:pass@host:/dir</targetPath>

<directory>..</directory> <include>..</include></resource>

Page 38: Maven Plugins

Upload files – FTP:

<resource>

<targetPath>ftp://user:pass@host:/dir</targetPath>

<directory>..</directory> <include>..</include></resource>

Page 39: Maven Plugins

Delete files:

<resource> <directory>..</directory> <include>..</include> <clean>true</clean>

<cleanEmptyDirectories>true</cleanEmptyDirectories>

</resource>

Page 40: Maven Plugins

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>

Page 41: Maven Plugins

Create directory:

<resource> <targetPath>..</targetPath> <mkdir>true</mkdir></resource>

Page 42: Maven Plugins

Groovy support - <runIf>:

<configuration> <runIf>{{ Groovy Expression

}}</runIf> <resource> <runIf>{{ Groovy Expression

}}</runIf> //

Boolean.valueOf( String.valueOf( returnValue ))

</resource></configuration>

Page 43: Maven Plugins

Groovy support - <description>:

<resource> <description>Time: {{ new Date()

}}</description> … </resource>

• Logs <resource> start, end, and execution time in ms.

Page 44: Maven Plugins

Groovy support - <replace>:

<resource> … <replace> <to>File updated at: {{ new Date()

}}</to> <groovy>true</groovy> </replace>

</resource>

Page 45: Maven Plugins

Groovy support – FTP download + <listFilter>:

<resource> FTP download <wget>wget.exe</wget>

<listFilter>{{ files.keySet().findAll{ .. } }}</listFilter>

</resource>

• Filters files to download.

Page 46: Maven Plugins

Groovy support - <filter>:

<resource> <targetPath>..</targetPath> … <filter>{{ files.findAll{ .. } }}</filter></resource>

• Filters files to copy, pack, unpack, etc.

Page 47: Maven Plugins

Groovy support - <process>:

<resource> <targetPath>..</targetPath> … <process>{{ println “Files: $files” }}</process>

</resource>

• Processes files after they are copied, packed, etc.

Page 48: Maven Plugins

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” ) }}

Page 50: Maven Plugins

• Creates new Maven properties at run time ..• .. evaluating Groovy expressions!• Works best in combination with <runIf>• Allows creating dynamic content for the mail

plugin

Page 51: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-properties-plugin</

artifactId><version>0.2.2</version>

Page 52: Maven Plugins

<configuration>:

<configuration> <properties> <property>..</property> <property>..</property> </properties></configuration>

Page 53: Maven Plugins

<property>:

<property> <name>propertyTrue</name> <value>{{ 't' + 'r' + 'u' + 'e' }}</value></property>…<runIf>{{ propertyTrue }}</runIf><runIf>{{ !

Boolean.valueOf( propertyTrue ) }}</runIf>

Page 54: Maven Plugins

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 }}

Page 56: Maven Plugins

• Generates Hudson/Jenkins jobs• Defines them in a single POM• Jobs can form hierarchicall groups (reuse!)• Supports jobs invocation• Supports Artifactory deployment

Page 57: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-jenkins-plugin</

artifactId><version>0.2.2</version>

Page 58: Maven Plugins

<configuration> :

<configuration>

<outputDirectory>.jenkins/jobs</outputDirectory>

<jobs> <job>..</job> <job>..</job> </jobs></configuration>

Page 59: Maven Plugins

<job> :

<job> <id>jobName</id>

<mavenName>apache-maven-2.2.1</mavenName> <mavenOpts>-Xmx256m</mavenOpts>

<mavenGoals>–e –B –U clean install</mavenGoals>

…</job>

Page 60: Maven Plugins

<job> :

• <jdkName>• <pom>• <repositories>• <privateRepository>• <mail>• …• All standard job configuration options.

Page 61: Maven Plugins

Extending <job> (1):

<job> <id>baseJob</id> <abstract>true</abstract> <jdkName>..</jdkName> <mavenName>..</mavenName> <mavenGoals>..</mavenGoals></job>

Page 62: Maven Plugins

Extending <job> (2):

<job> <id>jobName</id> <parent>baseJob</parent> <repository>..</repository></job>

• Only job-specific values are specified.

Page 63: Maven Plugins

Description Table:

Page 64: Maven Plugins

Invoking jobs:

<job> <id>jobName</id>

… <invoke> <jobs>jobA, jobB, jobC</jobs> </invoke></job>

Page 65: Maven Plugins

Invoking jobs - conditions:

<invoke> <jobs>jobA, jobB, jobC</jobs> <always>false</always> <stable>true</stable> <unstable>false</unstable> <failed>false</failed> </invoke>

Page 66: Maven Plugins

Artifactory deployment:

<job> <artifactory> <name>http://host/artifactory</name> <user>deployer</user>

<scrambledPassword>..</scrambledPassword>

</artifactory></job>

Page 67: Maven Plugins

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

Page 69: Maven Plugins

• Build assertions!• Properties defined• Files exist• Directories identical• Groovy expressions evaluate to true– Was driven by plugins tests

Page 70: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-assert-plugin</

artifactId><version>0.2.2</version>

Page 71: Maven Plugins

<configuration> :

<configuration> <assertProperties>..</assertProperties> <assertFiles>..</assertFiles> <assertEqual>..</assertEqual> <assertGroovy>..</assertGroovy></configuration>

Page 72: Maven Plugins

<assertProperties> :

<assertProperties> job-parameter BUILD_NUMBER JENKINS_URL</assertProperties>

• Makes sure all job parameters are specified.

Page 73: Maven Plugins

<assertFiles> :

<assertFiles> ${data-files}/*.xml ${project.build.directory}/setup.tar.gz ${project.build.directory}/ini</assertFiles>

• Makes sure all files and directories are created.

Page 74: Maven Plugins

<assertEqual> :

<assertEqual> ${dir}/expected|${outputDir}/result

${dir}/expected|${outputDir}/result|**/*.xml

</assertEqual>

• Makes sure directories are identical (+/- pattern).

Page 75: Maven Plugins

<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!)

Page 76: Maven Plugins

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

Page 78: Maven Plugins

• Sends e-mails with attachments from Maven

• Works great for build-time reports • Properties plugin allows for dynamic

content

Page 79: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-mail-plugin</

artifactId><version>0.2.2</version>

Page 80: Maven Plugins

<configuration>:

<configuration> <smtp>..</smtp> <from>..</from> <mails><to>..</to></mails> <subject>..</subject> <text>..</text></configuration>

Page 81: Maven Plugins

<mails>:

<mails> <to>user@mail; user2@mail</to> <cc>User Name &lt;user@mail&gt;;</cc> <bcc>..</bcc></mails>• Use &lt;user@mail&gt; or CDATA• Addresses are «;»-separated to allow spaces

in names

Page 82: Maven Plugins

Attaching files:

<configuration> <textFile>..</textFile> <files> <file>..</file> <file>..</file> </files></configuration>

Page 84: Maven Plugins

• Invokes SpringBatch job as part of Maven build

• Maven is a good platform for batch processes!

Page 85: Maven Plugins

<groupId>com.goldin.plugins</groupId><artifactId>maven-spring-batch-

plugin</artifactId><version>0.2.2</version>

Page 86: Maven Plugins

<configuration>:

<configuration> <jobId>jobId</jobId> <configLocations> classpath:/springbatch/infra/job-

launcher.xml file:/job/context.xml </configLocations></configuration>

Page 87: Maven Plugins

Properties:

<configuration> <props> name = value classpath:/springbatch/infra/job-

runner.properties file:/job/context.properties </props></configuration>