Pages

Sunday, June 19, 2016

Maven Plugins for a Multi-Modular Project

appassembler-maven-plugin


       End to end portability of applications is a vital fact when it comes to the industry level mainly. In Java it has always been a pain due to the missing OS abstractions.
    
    The main goal of the Appassembler Maven plug-in is to overcome this pain and to produce deployable daemons in various configured OSs.


      The Application Assembler Plugin is a Maven plugin for generating scripts for different OSs  for starting java applications.

   
                                                                             All artifacts
                                                        (dependencies + the artifact from the project)

                                                            classpath in the generated bin scripts.


  • Below shows the appassembler maven plugin

 <!-- http://mvnrepository.com/artifact/org.codehaus.mojo/appassembler-maven-plugin -->

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.3</version>
</dependency>

 


maven-assembly-plugin


Assembly =>  group of files, directories and dependencies that are assembled into ONE archieve.

              
        So Assembly Plugin for Maven is intended  to group the project output together with its dependencies, modules, site-documentations and other files into a one distributive archive. 

To use the Assembly Plugin in Maven, you simply need to:

  • choose or write the assembly descriptor to use(assembly.xml)
  • configure the Assembly Plugin in your project's pom.xml, and
  • run "mvn assembly:single" on your project.

assembly descripter

  • 2 types of descripters
1) Prefabricated Assembly descriptors
                                handle many common operation like packaging an artifact 
                                to a zip archive along with the site documentation 

              http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-  refs.html 

 Above link gives a good description on such descriptors


2) Your own descriptor
                                Other than the common assembly requirements, using this
                                 type  of descriptors we can specify 
                                                           - the type of archive to  make
                                                           - the content in the assembly
                                                           - the way the dependencies and modules are
                                                              bundled in assembly

<!-- maven assembly plugin. --><plugin>
    <artifactId>maven-assembly-plugin</artifactId>

    <executions>
        <execution>
            <id>zip</id>
            <phase>package</phase>
            <goals>
                <!-- Assemble an application bundle or distribution. -->                <goal>single</goal>
            </goals>

            <configuration>
                <!-- assembly.xml file contains the configurations of assembly plugin. -->                <descriptor>assembly.xml</descriptor>
            </configuration>
        </execution>
    </executions>
</plugin>
 
 
examples can be found in the following link
 http://maven.apache.org/plugins-archives/maven-assembly-plugin-2.6/examples/multimodule/index.html 
 
 important
http://maven.apache.org/plugins-archives/maven-assembly-plugin-2.6/examples/multimodule/index.html


maven-jar-plugin

               This plugin provides the capability to build jars.  The jar is the default packaging type.So it is not required to set it in most cases.

 

  • jar:jar create a jar file for your project classes inclusive resources.
  • jar:test-jar create a jar file for your project test classes .

 How to build a jar file 

When you want to create a jar file with your Maven project, first create a pom file with at least the below configs.
"jar" is the default packaging type. So not required to set in the above  case. Using the below command we can create a artifact

                                 mvn package

Generated jar file is in the target directory
The resulting jar ===>  compiled classes + src/main/resources





So what could be the use of the maven jar plugin..? 

    - You can customize the manifest by default. SInce the jar plugin uses Maven  Archiever , it does not create the implementation details in the manifest by default. Say explicitly in the plugin configuration as required

https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html

    -Can include and exclude content from the jar archive

https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html

    - Can create additional attached jar artifacts from the project 

https://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html

   - can create a jar containing test classes
https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html

 

maven-compiler-plugin

  • compiler plugin is used to compile the sources of your project.  

  • The default compiler is  javax.tools.JavaCompiler   which is used to compile the java sources




  • There are two main goals for the Maven plugin. Each of these goals are bound to their respective phases in the Maven Life Cycle. So they get automatically executed in those respective phases.
                          compiler:compile goal   =>    executed by the command    mvn compile
compiler:testCompile goal   => executed by the command     mvn test-compile
  • Below shows a simple configuaration of Maven Compiler Plugin

  • To compile sources with a different JDK, use the below configuration with fork set to true and compilerVersion with the version required




maven-antrun-plugin

Provides the ability to run ant tasks from within maven. Other than that anyrun plugin is also used to unpack the artifacts of the project


You can embed your ant scripts in the pom file. But it is encouraged to move all the Ant tasks to build.xml file call that file from pom.xml







No comments:

Post a Comment