Login  Register

How to create a fat Plugin.jar with Maven that includes dependencies.

Posted by Michael P Ellis on Nov 04, 2020; 4:19pm
URL: http://imagej.273.s1.nabble.com/How-to-create-a-fat-Plugin-jar-with-Maven-that-includes-dependencies-tp5024146.html

I am trying to create an ImageJ (1) plugin with maven. There is a dependency (log4J) and I am using the maven maven-shade-plugin to create a plugin with the intention that the plugin will be self-contained and include the necessary dependencies.

So far I have found that when this plugin is installed into a standard ImageJ installation and run from the command line (OS-X) with java -jar in,jar it fails with an error message indicating that it has been unable to locate the log4j libraries.

Manually installing copes of the log4j libraries in the ImageJ plugins/jar directory allows the plugin to run successfully and I get to see the log output on the terminal/

        I have created a SSCCE to demonstrate which can be seen here: https://gitlab.com/Michael51773/minimalimagejplugin (https://gitlab.com/Michael51773/minimalimagejplugin)

        I am not that experienced with maven so if the solution is maven related please assume I have the experience of a small child!

The ultimate use scenario will include a multitude of dependencies and not just the log4j.

I hope to release some of the plugins I am using for general public use and to be free of charge (so hope to put something back into the community)

Many thanks in anticipation of any help.

        --- MinimalImageJPlugin_.jar ---
import ij.IJ;
import ij.plugin.PlugIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MinimalImageJPlugin_ implements PlugIn {
 private static final Logger LOGGER = LogManager.getLogger();

 @Override
 public void run(String arg) {
 LOGGER.error("Hello");
 IJ.showMessage(String.format("MinimalImageJPlugin_ arg="%s"", arg));
 }
}

        --- pom.xml ---
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>biz.dsuk</groupId>
 <artifactId>MinimalImageJPlugin__</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>MinimalImageJ</name>
 <description>A minimal ImageJ PlugIn build with a maven with a dependency
on log4j to work out how to build a fat or uber jar that includes the
log4j jar dependency and can still be used as an ImageJ Plugin.</description>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>14</maven.compiler.source>
 <maven.compiler.target>14</maven.compiler.target>
 </properties>
 <dependencies>
 <dependency>
 <groupId>net.imagej</groupId>
 <artifactId>ij</artifactId>
 <version>1.53e (DS)</version>
 </dependency>

 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-api</artifactId>
 <version>2.13.3</version>
 <scope>compile</scope>
 <type>jar</type>
 </dependency>
 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 <version>2.13.3</version>
 <scope>compile</scope>
 <type>jar</type>
 </dependency>
 </dependencies>
 <build>
 <finalName>SC4_</finalName>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-shade-plugin</artifactId>
 <!-- see https://github.com/AdoptOpenJDK/openjdk-installer/issues/93 -->
 <version>3.2.4</version>
 <executions>
 <execution>
 <phase>package</phase>
 <goals>
 <goal>shade</goal>
 </goals>
 <configuration>
 <artifactSet>
 <excludes>
 <exclude>net.imagej:ij</exclude>
 </excludes>
 </artifactSet>
 </configuration>
 </execution>
 </executions>
 </plugin>
 </plugins>
 </build>
</project>

--- END ---

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html