Using poi-ooxml dependency in Maven plugin project

plugin
Tags: #<Tag:0x00007fa30749b3d8>

#1

Hello,

I am building a plugin with Maven and would like to use the Apache poi-ooxml library to work with .xls files.
I am currently playing around with the example plugin https://github.com/imagej/example-legacy-plugin to learn how to use Maven.
I have imported the project into Eclipse and have added the dependency to the pom:

org.apache.poi poi-ooxml 3.17

I am able to use all the functionality such as Workbook, Sheet, and Cell for my purposes in Eclipse.
However when packaging my project through command line (mvn package), I run into an Enforcer Rule error (" Rule 2: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message: No Duplicate Classes Allowed!").
I am warned that I have duplicate classes in apache xmlbeans, which I have tried excluding in my dependencies. This does yield a successful compile however I am unable to use some of the classes from poi-ooxml that I wanted.
I have also tried running the -Denforcer.skip=true flag but this does not seem to package my project correctly (Imagej is not able to find my main class files when I drag my jar file into the plugins folder).
The only other dependencies I have included is net.imagej ij which was included in the example project.
Any advice on using poi-ooxml in imagej plugins?

Thanks,
Christian


#2

Hi Christian,

I have used Apache POI in several ImageJ Eclipse projects but I do not have much experience using Maven. For my previous imagej1.x plugins, I have manually organised my project to create the necessary folder hierarchies and resources, including adding any required libraries as dependencies myself. However, in reaction to seeing your post on Friday, I tried to familiarise myself with the Maven workflow. I believe I now have a mavenised version of an imagej1.x POI plugin. Fortunately (and unfortunately), I could not encounter/replicate your issue.

Perhaps if you use my modified ‘example-legacy-plugin-master’ project folder as a starting point it may help:
https://drive.google.com/file/d/15f3bTW8UWhNevic__lg-e7QBebVsMVQc/view?usp=sharing
This also contains the plugin itself, which includes data parsing and POI specific java, which may be useful to your project. For instance, the plugin can scan imported column headers for a specific String (like ‘Area’), then create an excel formula formatted cell underneath the last data point in this column, containing the usual excel bluff (like ‘=STDEV.P(B2:B10)’ etc). It also creates and appends the same file in the specified location, which is a function I included in my previously released plugins.

Hopefully, the project folder will give you an idea for what worked for me (I also tried to annotate the code extensively but I’m not a programmer by trade so please excuse informal wording etc). Briefly here, I would say that you may need more than only the ‘poi-ooxml’ library added as a dependency for your plugin to work. At a minimum I have required the following APACHE libraries:
poi
poi-ooxml
poi-ooxml-schemas
commons-collections4
XMLBeans
Although, this may be related to some extra functionality in my plugins. Also, sorry if you did include these extra libraries but just didn’t mention them in your post, as irrelevant.

Kind regards.


#3

Hi antinos,

Thanks for the reply. I have tried running the jar file “Poi_Example-1” in ImageJ and in Fiji however I am encountering an error when I try to run the plugin:

Plugin or class not found: “imagej.poiexample.Poi_Example” (java.lang.ClassNotFoundExcecption: imagej.poiexample.Poi_Example)

With my own project, I am able to compile and run the plugin through Eclipse as you are able to do with the example you provided me. (I have no issues with importing libraries)
However when I use the jar file produced by Eclipse in the standalone ImageJ application, I encounter this same class not found exception above.
I was wondering if this was an issue purely with how I am importing plugins to ImageJ. Would you be able to explain to me how you were able to run your plugin through ImageJ without Eclipse?
From my knowledge, I should be able to just drag and drop the jar file to the ImageJ plugins folder and it should run fine?

Thanks,


#4

Hi cmatchan,

Normally you should be able to drop the built .jar file into the plugins folder. Even if imagej can then identify the relevant class file(s), the plugin may still not work if any dependency .jar files are missing from the ‘jar’ folder. In your case, it looks like imagej could not find the ‘POI_Example’ class (the main plugin class). I think this is partly to do with the wierd folder structure that I instantiated for the build but, as you rightly state, the plugin worked for me running it in a standalone instance of magej. Not being an expert in these matters, and only familiarising myself with Maven for this query, I might suggest the following:

  • Did you copy the .jar file from the ‘target’ folder into your imagej plugin folder? You mentioned copying the .jar, but you might not have copied the ‘target’ folder version.
  • You may also need to check and modify the ‘plugins.config’ file inside of the project. This specifies the menu location of the plugin in imagej, but also importantly the location of relevant class file(s).
  • Maybe best option, could you build the plugin yourself by importing the modified maven template I provided the link for into eclipse? You’d need to ensure that your ‘Run As :arrow_forward: Run Configurations…’ parameters are correct for your system. See the Option 2: Install dependencies section of https://imagej.net/Developing_ImageJ_in_Eclipse
    Then build the project as I am sure you have already tried for your own plugin by ‘Run As :arrow_forward: Maven Build’
  • Finally, you may need to manually introduce your dependecy .jar files into the ‘jar’ folder of imagej. To do this, go to https://mvnrepository.com/ and search for the libraries I mentioned previously. Go to the latest version of each until an option is given to download the .jar bundle. Move these into the ‘jar’ folder. Normally, the maven build should handle this if you have specified the correct imagej location in the Eclipse configurations parameters but I am just offering the above advice to get the plugin working, if the build does not complete (as it didn’t for me on several occasions).

If anybody who reads the above, and knows a little more about Maven, wants to chip in to offer advice or say how I might be saying something silly, also feel free.

Regards.


#5

Hi cmatchan,

Just a quick follow-up to say that the ‘Poi_example-1’ jar should work as I provided it (I just tested it in my work build of Fiji). The plugin can be run from ‘Plugins>POI>POI Example’. If you don’t rebuild it yourself in Eclipse though, you will also need to manually add the dependency jars to the jars folder as I mentioned previously.


#6

Hi antinos,

I tried manually adding the dependencies you specified previously into the ImageJ jar folder which resolved the ClassNotFoundException error I was getting. However now I am getting a NullPointerException when I run the plugin (I am able to see the Plugins>POI>POI Example in the menu in ImageJ).

ImageJ 1.52a; Java 1.8.0_112 [64-bit]; Windows 10 10.0; 54MB of 12207MB (<1%)

java.lang.NullPointerException
at org.apache.poi.ss.util.CellReference.(CellReference.java:148)
at org.imagej.poiexample.Poi_Example$Results_To_Excel.run(Poi_Example.java:201)
at org.imagej.poiexample.Poi_Example.run(Poi_Example.java:35)
at ij.IJ.runUserPlugIn(IJ.java:221)
at ij.IJ.runPlugIn(IJ.java:185)
at ij.Executer.runCommand(Executer.java:137)
at ij.Executer.run(Executer.java:66)
at java.lang.Thread.run(Thread.java:745)

This error seems to arise from issues referencing the CellReference dependency from apache poi from looking at your source code.
I tried rebuilding your project myself in Eclipse and have set up Eclipse as specified in the “Developing ImageJ in Eclipse” tutorial.
When building my project, I come across the same enforcer rule error as in my original post.

Rule 2: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message:
No Duplicate Classes Allowed!

  • For duplicate transitive dependencies, add dependency exclusions.

  • For duplications between direct dependencies, resolve or add
    ignored classes to this rule’s configuration.

    Found in:
    org.apache.xmlbeans:xmlbeans:jar:2.6.0:compile
    Duplicate classes:
    org/apache/xmlbeans/xml/stream/XMLName.class
    org/apache/xmlbeans/xml/stream/XMLEvent.class
    org/apache/xmlbeans/xml/stream/utils/NestedThrowable.class
    org/apache/xmlbeans/xml/stream/XMLInputStream.class
    org/apache/xmlbeans/xml/stream/XMLStreamException.class
    org/apache/xmlbeans/xml/stream/utils/NestedThrowable$Util.class
    org/apache/xmlbeans/xml/stream/ReferenceResolver.class
    org/apache/xmlbeans/xml/stream/Location.class

I am thinking I may need to use the Maven shade plugin since there seems to be duplicate dependencies on these xmlbeans classes for both net.imagej and one of the other third party dependencies you are using.

I am also wondering if anyone else is able reproduce this Duplicate Class exception I am getting by building antinos’s project through Eclipse themselves? Or if you are able to run the plugin by manually adding dependencies to the ImageJ jar folder and dropping the jar file in the plugins folder?


#7

Hi,

Try running the plugin with a results table open. I didn’t try to design a full fledged plugin for this example, as i only wanted to show a working implementation of a mavenised plugin that uses the Apache POI libraries. You originally mentioned developing your own plugin so i thought working java would be instructive but i didn’t attempt to handle even normal exceptions like what to do when not having a results table open when the plugin is called.
If you do want a fully fledged ‘off the shelf’ version, i have released a ‘Read and Write Excel’ plugin that can be installed via the update manager.

Best wishes.

EDIT
sorry, i am replyingg via my phone at the moment and only caught the top part of your message. My reply will probably address the Null Pointer Exception but without further testing at my computer, and since i couldn’t replicate the Duplicate Dependency issue myself earlier, im still not sure of the problem there. I can think about it some more later but if someone else did want to chip in as OP suggests, then that’s also welcome.