Puttygen usage

This code will execute a new Maven build to the install lifecycle phase for the project defined at /path/to/pom.xml. If the build fails, we will remain blissfully ignorant...

Checking the Exit Code

If we wanted to detect a build failure in the above example, we could simply add the following lines:

InvocationResult result = invoker.execute( request ); if ( result.getExitCode != 0 ) { throw new IllegalStateException( "Build failed." ); }

This will retrieve the exit code from the invocation result, and throw an exception if it's not 0 (the traditional all-clear code). Note that we could capture the build output by adding an InvocationOutputHandler instance to either the invoker or the request.

Caching the Invoker

Since you can specify global options for Maven invocations via the Invoker configuration, it will often make sense to configure a single Invoker instance, and reuse it over multiple method calls:

// we will always call the same goals... private static final List PUBLISH_GOALS = Arrays.asList( "clean", "site-deploy" ); // define a field for the Invoker instance. private final Invoker invoker; // now, instantiate the invoker in the class constructor... public SomeClass( File localRepositoryDir ) { Invoker newInvoker = new DefaultInvoker; newInvoker.setLocalRepositoryDirectory( localRepositoryDir ); this.invoker = newInvoker; } // this method will be called repeatedly, and fire off new builds... public void publishSite( File siteDirectory ) throws PublishException { InvocationRequest request = new DefaultInvocationRequest; request.setBaseDirectory( siteDirectory ); request.setInteractive( false ); request.setGoals( PUBLISH_GOALS ); InvocationResult result = invoker.execute( request ); if ( result.getExitCode != 0 ) { if ( result.getExecutionException != null ) { throw new PublishException( "Failed to publish site.", result.getExecutionException ); } else { throw new PublishException( "Failed to publish site. Exit code: " + result.getExitCode ); } } }

As you can see, we're using the same local repository location (since the site-generation artifacts will most likely be common to most sites), the same invoker instance (it's configured, we may as well reuse it), and the same set of goals per build. We can actually accommodate a fairly complex configuration of the Invoker without adding complexity to the publishSite method in this manner.

Puttygen program

Puttygen online

Puttygen heise

Puttygen rsa2

Puttygen.exe help