// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

[[ugr.tools.pear.packager.maven.plugin.usage]]
= The PEAR Packaging Maven Plugin

UIMA includes a Maven plugin that supports creating PEAR packages using Maven.
When configured for a project, it assumes that the project has the PEAR layout,  and will copy the standard directories that are part of a PEAR structure under the project root into the PEAR, excluding files that start with a period (".").   It also will put the Jar that is built for the project into the lib/ directory and include it first on the generated classpath. 

The classpath that is generated for this includes the artifact's Jar first, any user specified entries second (in the order they are specified), and finally, entries for all Jars  found in the lib/ directory (in some arbitrary order). 

[[ugr.tools.pear.packager.maven.plugin.usage.configure]]
== Specifying the PEAR Packaging Maven Plugin

To use the PEAR Packaging Plugin within a Maven build,  the plugin must be added to the plugins section of the  Maven POM as shown below: 


[source]
----
<build>
 <plugins>
  ...
  <plugin>
    <groupId>org.apache.uima</groupId>
    <artifactId>PearPackagingMavenPlugin</artifactId>
    
    <!-- if version is omitted, then --> 
    <!-- version is inherited from parent's pluginManagement section -->
    <!-- otherwise, include a version element here --> 
    
    <!-- says to load Maven extensions 
         (such as packaging and type handlers) from this plugin -->
    <extensions>true</extensions>  
    <executions>
      <execution>
        <phase>package</phase>
        <!-- where you specify details of the thing being packaged -->
        <configuration>  
          
          <classpath>
            <!-- PEAR file component classpath settings -->
            $main_root/lib/sample.jar
          </classpath>
          
          <mainComponentDesc>
            <!-- PEAR file main component descriptor -->
            desc/${artifactId}.xml
          </mainComponentDesc>
          
          <componentId>
            <!-- PEAR file component ID -->
            ${artifactId}
          </componentId>
          
          <datapath>
            <!-- PEAR file UIMA datapath settings -->
            $main_root/resources
          </datapath>
          
        </configuration>
        <goals>
          <goal>package</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  ...
 </plugins>
</build>
----

To configure the plugin with the specific settings of a PEAR package, the `<configuration>` element section is used.
This sections contains all parameters  that are used by the PEAR Packaging Plugin to package the right content and set the specific PEAR package settings.
The details about each parameter and how it is used is shown below: 

* `<classpath>` - This element specifies the classpath settings for the  PEAR component. The Jar artifact that is built during the current Maven build is  automatically added to the PEAR classpath settings and does not have to be added manually. In addition, all Jars in the lib directory and its subdirectories will be added to the generated classpath when the PEAR is installed. 
+
[NOTE]
====
Use $main_root variables to refer to libraries inside  the PEAR package.
For more details about PEAR packaging please refer to the  Apache UIMA PEAR documentation.
====

* `<mainComponentDesc>` - This element specifies the relative path to the main component descriptor  that should be used to run the PEAR content. The path must be relative to the  project root. A good default to use is ``desc/${artifactId}.xml``. 
* `<componentID>` - This element specifies the PEAR package component ID. A good default to use is ``${artifactId}``. 
* `<datapath>` - This element specifies the PEAR package UIMA datapath settings. If no datapath settings are necessary, this element can be omitted. 
+
[NOTE]
====
Use $main_root variables to refer libraries inside  the PEAR package.
For more details about PEAR packaging please refer to the  Apache UIMA PEAR documentation.
====

For most Maven projects it is sufficient to specify the parameters described above.
In some cases, for  more complex projects, it may be necessary to specify some additional configuration  parameters.
These parameters are listed below with the default values that are used if they are not  added to the configuration section shown above. 

* `<mainComponentDir>` - This element specifies the main component directory where the UIMA nature is applied. By default this parameter points to the project root  directory - ${basedir}. 
* `<targetDir>` - This element specifies the target directory where the result of the plugin  are written to. By default this parameters points to the default Maven output  directory - ${basedir}/target 


[[ugr.tools.pear.packager.maven.plugin.usage.dependencies]]
== Automatically including dependencies

A key concept in PEARs is that they allow specifying other Jars in the classpath.
You can optionally include these Jars within the PEAR package. 

The PEAR Packaging Plugin does not take care of automatically adding these Jars (that the PEAR might depend on) to the PEAR archive.
However, this behavior can be manually added to your Maven POM.
The following two build plugins hook into the build cycle and insure that all runtime dependencies are included in the PEAR file. 

The dependencies will be automatically included in the  PEAR file using this procedure; the PEAR install process also will automatically adds all files in the lib directory (and sub directories) to the  classpath. 

The `maven-dependency-plugin` copies the runtime dependencies of the PEAR into the `lib` folder, which is where the PEAR packaging plugin expects them. 

[source]
----
<build>
 <plugins>
  ...
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
    <!-- Copy the dependencies to the lib folder for the PEAR to copy -->
    <execution>
     <id>copy-dependencies</id>
     <phase>package</phase>
     <goals>
      <goal>copy-dependencies</goal>
     </goals>
     <configuration>
      <outputDirectory>${basedir}/lib</outputDirectory>
      <overWriteSnapshots>true</overWriteSnapshots>
      <includeScope>runtime</includeScope>
     </configuration>
    </execution>
   </executions>
  </plugin>
  ...
 </plugins>
</build>
----

The second Maven plug-in hooks into the `clean` phase of the build life-cycle, and deletes the `lib` folder. 

[NOTE]
====
With this approach, the `lib` folder is  automatically created, populated, and removed during the build process.
Therefore it should not go into the source control system and neither should you manually place any jars in there. 
====

[source]
----
<build>
 <plugins>
  ...
  <plugin>
   <artifactId>maven-antrun-plugin</artifactId>
   <executions>
    <!-- Clean the libraries after packaging -->
    <execution>
     <id>CleanLib</id>
     <phase>clean</phase>
     <configuration>
      <tasks>
       <delete quiet="true" 
               failOnError="false">
        <fileset dir="lib" includes="**/*.jar"/>
       </delete>
      </tasks>
     </configuration>
     <goals>
      <goal>run</goal>
     </goals>
    </execution>                      
   </executions>
  </plugin>
  ...
 </plugins>
</build>
----

[[ugr.tools.pear.packager.maven.plugin.commandline]]
== Running from the command line

The PEAR packager can be run as a maven command.
To enable this, you have to add the following to your maven settings file: 

[source]
----
<settings>
  ...
  <pluginGroups>
    <pluginGroup>org.apache.uima</pluginGroup>
  </pluginGroups>
----
To invoke the PEAR packager using maven, use the command: 

[source]
----
mvn uima-pear:package <parameters...>
----

The settings are the same ones used in the configuration above, specified as -D variables  where the variable name is `pear.parameterName``.
For example: 
[source]
----
mvn uima-pear:package -Dpear.mainComponentDesc=desc/mydescriptor.xml
                      -Dpear.componentId=foo
----

[[ugr.tools.pear.packager.maven.plugin.install.src]]
== Building the PEAR Packaging Plugin From Source

The plugin code is available in the Apache subversion repository at: http://svn.apache.org/repos/asf/uima/uimaj/trunk/PearPackagingMavenPlugin.
Use the following command line to build it (you will need the Maven build tool, available from Apache): 


[source]
----
#PearPackagingMavenPlugin> mvn install
----

This maven command will build the tool and install it in your local maven repository,  making it available for use by other maven POMs.
The plugin version number is displayed at the end of the Maven build as shown in the example below.
For this example, the plugin  version number is: `2.3.0-incubating`

[source]
----
[INFO] Installing 
/code/apache/PearPackagingMavenPlugin/target/
PearPackagingMavenPlugin-2.3.0-incubating.jar 
to 
/maven-repository/repository/org/apache/uima/PearPackagingMavenPlugin/
2.3.0-incubating/
PearPackagingMavenPlugin-2.3.0-incubating.jar
[INFO] [plugin:updateRegistry]
[INFO] --------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] --------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Tue Nov 13 15:07:11 CET 2007
[INFO] Final Memory: 10M/24M
[INFO] --------------------------------------------------------------
----