Maven jar Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 8: Line 8:


=Overview=
=Overview=
=Plugin Options=
==forceCreation==
If set to true, the jar plugin will build a new JAR even if none of the contents appear to have changed. By default, this plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the plugin skips creation of the jar.


=Declaring a Main Class in JAR=
=Declaring a Main Class in JAR=
Line 15: Line 21:
         <groupId>org.apache.maven.plugins</groupId>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <artifactId>maven-jar-plugin</artifactId>
         ...
         <version>3.0.2</version>
         <configuration>
         <configuration>
           <archive>
           <archive>
Line 24: Line 30:
           </archive>
           </archive>
         </configuration>
         </configuration>
        ...
       </plugin>
       </plugin>
</pre>
</pre>


=Other Manifest Customization=
=Other Manifest Customization=
Line 34: Line 38:


<pre>
<pre>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
      <archive>
        <manifest>
            <mainClass>io.novaordis.eventagent.EventAgent</mainClass>
        </manifest>
        <manifestEntries>
            <Premain-Class>io.novaordis.eventagent.EventAgent</Premain-Class>
            <Agent-Class>io.novaordis.eventagent.EventAgent</Agent-Class>
            <Can-Redefine-Classes>true</Can-Redefine-Classes>
            <Can-Retransform-Classes>true</Can-Retransform-Classes>
        </manifestEntries>
      </archive>
  </configuration>
</plugin>
</pre>
</pre>


Line 41: Line 62:
...
...
<manifest>
<manifest>
  <manifestEntries>
      ...
      <key>value</key>
      ...
  </manifestEntries>
</manifest>
</manifest>
<manifestEntries>
  ...
  <key>value</key>
  ...
</manifestEntries>
</pre>
</pre>
=Include Dependencies - or Some of the Dependencies - in JAR=
There are situations when we want to extract some of the dependencies and bundle them with the JAR artifact. This is done using the jar plugin and the dependency plugin together, as described here:
{{Internal|Maven Include Dependencies in JAR Artifact|Include Dependencies in JAR Artifact}}
=Explicitly Include Content in the JAR=
{{External|http://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html}}
<syntaxhighlight lang='xml'>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
    <includes>
      <include>**/service/*</include>
    </includes>
    <excludes>
      <exclude>**/something/*</exclude>
    </excludes>
  </configuration>
</plugin>
</syntaxhighlight>
The patterns need to be relative to to the path specified for the plugin's classesDirectory parameter.

Latest revision as of 18:36, 2 March 2018

External

Internal

Overview

Plugin Options

forceCreation

If set to true, the jar plugin will build a new JAR even if none of the contents appear to have changed. By default, this plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the plugin skips creation of the jar.

Declaring a Main Class in JAR

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

Other Manifest Customization

Adding arbitrary manifest entries:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <configuration>
      <archive>
         <manifest>
            <mainClass>io.novaordis.eventagent.EventAgent</mainClass>
         </manifest>
         <manifestEntries>
            <Premain-Class>io.novaordis.eventagent.EventAgent</Premain-Class>
            <Agent-Class>io.novaordis.eventagent.EventAgent</Agent-Class>
            <Can-Redefine-Classes>true</Can-Redefine-Classes>
            <Can-Retransform-Classes>true</Can-Retransform-Classes>
         </manifestEntries>
      </archive>
   </configuration>
</plugin>

In general, to use arbitrary manifest entries, use the following syntax:

...
<manifest>
</manifest>
<manifestEntries>
   ...
   <key>value</key>
   ...
</manifestEntries>

Include Dependencies - or Some of the Dependencies - in JAR

There are situations when we want to extract some of the dependencies and bundle them with the JAR artifact. This is done using the jar plugin and the dependency plugin together, as described here:

Include Dependencies in JAR Artifact

Explicitly Include Content in the JAR

http://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
    <includes>
      <include>**/service/*</include>
    </includes>
    <excludes>
      <exclude>**/something/*</exclude>
    </excludes>
  </configuration>
</plugin>

The patterns need to be relative to to the path specified for the plugin's classesDirectory parameter.