Apache Maven - Still Rocking It

A Dozen Demos and Great News for Apache Maven Users

Jason van Zyl

Manfred Moser

Tip Hold on tight, this is going to be a wild ride!

What Are You Going to Learn?

  • Tips & tricks
  • Best practices
  • New tools, plugins, …

→ Lots of stuff happening in the Apache Maven ecosystem!

Before We Get Started

  • The developers of Maven are opinionated…
  • And so is Maven itself
  • Follow best practices, to make your life easier

And…

  • Maven is open source
  • Work happens on latest code
  • Old releases receive little attention or support
  • There is no one company behind Maven - its a community!

Do NOT Use Old Maven Versions

  • Maven 1 - doh!
  • Maven 2 - seriously, just don’t do it!
  • Maven Ant Tasks - nope, based on Maven 2 and unmaintained
  • Maven 3.0.x - old but stable
  • Maven 3.1.x - old with issues
  • Maven 3.2.x - new but not latest supported
  • None of the above should be used
  • Instead keep up to date with newer releases
Tip Unless you are prepared to maintain those projects yourself, use Maven 3.3.3 or 3.3.7

Avoid Other Old Stuff

Be very careful about

  • IDEs using old Maven versions
  • Using different version on command line / CI server
  • Old IDE plugins/integrations
  • Old Maven plugins
  • JDK 4, 5, and 6 usage

→ Continuing to use old stuff causes rotting.

Now Let’s Get Started

We will look at things that …

  • Simplify project build
  • Control project build
  • Improve development
  • And build performance
  • Provide new tools
  • And much more

Where Are We At?

  • Apache Maven 3.3.3/3.3.7
  • Apache Maven Plugins - require 3+
  • Java 1.7+ for build execution
  • Codehaus Plugins migrated to MojoHaus

Misc Newer Small Features

  • JSR330-based injection for extensions and plugins
  • Transitive dependency <exclusions> with *
  • Pluggable reactor
  • Version ranges in <parent>
  • Central Repository access defaults to HTTPS

Maven Wrapper

  • Shameless copy of the Gradle Wrapper
  • Unix and Windows scripts
  • Within project structure → version controlled
  • Automatic download and install of Maven
  • Uses ~/.m2/wrapper as cache
  • Removes need to provision Maven
  • For developer and CI server provisioning
  • Specifies Maven version

Maven Wrapper

mvn -N io.takari:maven:wrapper

And then, just use similar commands to

./mvnw clean install
mvnw.cmd clean install

Project Local Configuration

.mvn directory

  • Project local
  • In root folder of multi-module project
  • Version controlled with source
  • Maven 3.3.1+
  • Controls how project is built

Used for

  • JVM configuration
  • Maven configuration
  • Core extension loading

.mvn JVM Configuration

  • No more global, polluted MAVEN_OPTS
  • .mvn/jvm.config:
-Xmx8192m

.mvn Maven Configuration

  • .mvn/maven.config
  • Maven invocation parameters
-T 8
--builder smart
-U

Core Extension Loading

Allow different behavior of Maven itself

  • Larger scope and impact than plugins
  • Local repository access
  • Module ordering in reactor
  • Parse POM in different syntax - Polyglot Maven
Warning Large potential, but be careful…

Core Extensions Loading

.mvn/extensions.xml:

<?xml version="1.0" encoding="UTF-8"?>
<extensions>
  <extension>
    <groupId>io.takari.aether</groupId>
    <artifactId>takari-concurrent-localrepo</artifactId>
     <version>0.0.7</version>
  </extension>
  <extension>
    <groupId>io.takari.maven</groupId>
    <artifactId>takari-smart-builder</artifactId>
    <version>0.4.1</version>
  </extension>
</extensions>

Control Your Build

Beyond wrapper and .mvn

Back to the shiny, new extensions…

Concurrent Local Repo

  • Multiple builds running
  • E.g. on CI server
  • Using install

→ Can corrupt maven-metadata, mix snapshot versions,…

Tip Takari extension to avoid problems

Reactor Improvements

Considerably reduced multi-module build times.

images/smart-builder-scheduler.png

Reactor Improvements

Use extension or install in lib

Traditional Parallel
mvn -T 4 clean deploy
Takari Smart Builder
mvn clean deploy --builder smart -T4
Tip Check out the documentation.

Polyglot Maven

  • Support for POM written in
    • Ruby
    • YAML
    • Groovy
    • Others
  • Produces POM for deployment and tool support
  • Project and Examples available
  • Short and clear
  • Take advantage of language/markup

Polyglot Example - JRuby Build

  • Uses Maven
  • And Maven Wrapper
  • Polyglot Maven pom.rb
  • JRuby developers working on polyglot!
<extension>
  <groupId>io.takari.polyglot</groupId>
  <artifactId>polyglot-ruby</artifactId>
  <version>0.1.13</version>
</extension>

pom.rb

version = File.read( File.join( basedir, 'VERSION' ) ).strip
project 'JRuby', 'https://github.com/jruby/jruby' do
  model_version '4.0.0'
  inception_year '2001'
  id 'org.jruby:jruby-parent', version
  inherit 'org.sonatype.oss:oss-parent:7'
  packaging 'pom'
  organization 'JRuby', 'http://jruby.org'
  [ 'headius', 'enebo', 'wmeissner', 'BanzaiMan', 'mkristian' ].each do |name|
    developer name do
      name name
      roles 'developer'
    end
  end
...

Polyglot Example - SnakeYAML

  • Uses Maven
  • And Maven Wrapper
  • Polyglot Maven pom.yaml
  • SnakeYAML developers helping on polyglot!
<extension>
  <groupId>io.takari.polyglot</groupId>
  <artifactId>polyglot-yaml</artifactId>
  <version>0.1.13</version>
</extension>
Tip See the source.

pom.yaml

modelVersion: 4.0.0
groupId: org.yaml
artifactId: snakeyaml
version: 1.17-SNAPSHOT
packaging: jar
properties: {project.scm.id: bitbucket, project.build.sourceEncoding: UTF-8}
name: SnakeYAML
...
dependencies:
- {artifactId: junit, groupId: junit, optional: false, scope: test, type: jar, version: '4.12'}
- {artifactId: spring, groupId: org.springframework, optional: false, scope: test,
  type: jar, version: 2.5.6}

Polyglot Example - Groovy

  • Uses Maven
  • And Maven Wrapper
  • Polyglot Maven pom.groovy
  • GMaven developers helping on polyglot!
<extension>
  <groupId>io.takari.polyglot</groupId>
  <artifactId>polyglot-groovy</artifactId>
  <version>0.1.13</version>
</extension>

pom.groovy

project {
  modelVersion '4.0.0'
  groupId 'io.takari.polyglot'
  artifactId 'groovy-project'
  version '0.0.1-SNAPSHOT'
  build {
    $execute(id: 'hello', phase: 'validate') {
      println "Groovy, meet Maven"
    }
  }
}

Incremental Build

  • Dedicated, generic API for incremental builds
  • Tracks inputs, processes and outputs
  • Maven-specific implementation included

→ can be used to make Maven plugins support incremental behaviour easily

Tip Source on GitHub

Takari Lifecycle

New Maven Plugin

  • Uses incremental support library for sources and resources
  • One plugin for whole lifecycle
  • Replaces default lifecycle plugins (resources, compiler, jar, install, deploy)
  • Uses extensions
  • Packaging pom, takari-jar, takari-maven-plugin, takari-maven-component
  • Eclipse Java compiler for improved incremental compilation
Tip Documentation and source are open source.

Takari Lifecycle

Example simple usage:

<packaging>takari-jar</packaging>

...

<build>
  <plugins>
    <plugin>
      <groupId>io.takari.maven.plugins</groupId>
      <artifactId>takari-lifecycle-plugin</artifactId>
      <extensions>true</extensions>
    </plugin>
  </plugins>
</build>

Maven Testing Tools and Beyond

  • Maven plugin testing support
  • Test project generation from dot files
  • Proto plugin - archetype replacement

Plugin Testing

Maven plugin testing framework including IDE support:

  • Unit testing
  • Integration testing
<dependency>
  <groupId>io.takari.maven.plugins</groupId>
  <artifactId>takari-plugin-testing</artifactId>
  <version>${takari.test.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.takari.maven.plugins</groupId>
  <artifactId>takari-plugin-integration-testing</artifactId>
  <version>${takari.test.version}</version>
  <type>pom</type>
  <scope>test</scope>
</dependency>
Tip Check the docs

Integration Testing Examples

  • Multiple invocations of Maven builds from Maven build
  • Android Maven Plugin and NDK Plugin
  • Replacement of invoker usage
@RunWith(MavenJUnitTestRunner.class)
@MavenVersions({"3.0.5","3.3.3"})
public class HelloFlashLightSampleIT {

Maven Development Tools

  • M2Eclipse
  • MavenDevTools
  • TakariLifecycle support

Benefits:

  • Workspace resolution for
    • Dependencies
    • Plugins
    • Extensions
    • Maven itself
  • Maven Junit Test UI
  • Maven build view
Tip Demo time!

Demos

Eclipse and M2Eclipse

Other Cool Stuff

Lots of things happening:

Maven Central

  • Largest Maven2 format repository
  • High performance, global CDN
  • Default in Apache Maven and others
  • HTTPS

→ Sponsored by Sonatype

And the components come from…

OSSRH and Forges

Input funnel for Central Repository

  • OSSRH - large deployment of Nexus Repository Manager
  • Apache, JBoss, java.net … - secondary Nexus instances
  • Community support - on-boarding and documentation
Tip Feedback about docs is welcome!

More OSSRH and Central Stats

  • > 17 billion requests in 2014
  • > 1 million GAV coordinates
  • Currently about 100k projects total
  • Approx. 3000 new projects each month (GA)
  • 10 - 30 project verified and onbarded per day
  • Approx. 30.000 new releases each month (GAV)

One Last Thing

Check out what Jason and Ludo at Google have been working on …

Maven Central - Next

Full replication to Google Cloud Storage:

  • Playground for data-mining and exploration
  • Experiments with new features like
    • HTTP/2 (evolved SPDY)
    • Search service
    • Binary compatibility index
    • Hosted on same Google as Compute Engine, faster!
    • more..
Tip Contact us, if you want to play with it.

Google and Maven Central

It is live NOW!

Update $HOME/.m2/settings.xml to:

<settings>
  <mirrors>
    <mirror>
      <id>google-maven-central</id>
      <name>Google Maven Central</name>
      <url>https://maven-central.storage.googleapis.com</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
</settings>

Or add it as proxy repository in your repository manager.

Summary

  • Lots of things are moving
  • We only touched the tip of the iceberg
  • What did you contribute?
Tip Join us on the Maven users mailing list and beyond!

Next?

Join us for a Maven Hangout On Air

  • Demo your solution or tip
  • Ask your question
  • Discuss user questions
  • And developer questions

The End

Questions, Remarks & Discussion

Tip Slides and examples at http://takari.github.io/javaone2015/

Resources