jasperreports_extension.properties overwritten by

2019-01-15 22:03发布

问题:

In order to have an executable jar I include the maven dependencies. Everything works fine but the jasperreports_extension.properties. Jasper already has a default one that replaces mine.

I'd like to know how to combine both files (the default and custom one) into the jar file. Actually I have manually combined both settings and now I want to find the way to replace the file that assembly plugin copies with the merged one I already have.

This is my current maven assembly plugin settings:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>${maven-assembly-plugin.version}</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            com.test.sample.MainClass
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

These are my custom settings to add some fonts to the reports:

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory 
net.sf.jasperreports.extension.simple.font.families.ireportfamily1453367638844=fonts/fontsfamily1453367638844.xml

And the dependency:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.2.0</version>
</dependency>

EDIT:

Considering the dependencies:

  • jasperreports_extension.properties is inside (root level) the jasperreports-6.2.0.jar
  • jasperreports_extension.properties is inside (root level) the jasperreports-fonts-6.0.0.jar (this is only a Test Dependencies)

After I replaced the assembly plugin by shade one, the initial issue I reported is fixed, however I got this new one:

    Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry org/bouncycastle/mail/smime/SMIMEEnvelopedGenerator$EnvelopedGenerator.class
        at sun.security.util.SignatureFileVerifier.verifySection(Unknown Source)
        at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
        at sun.security.util.SignatureFileVerifier.process(Unknown Source)
        at java.util.jar.JarVerifier.processEntry(Unknown Source)
        at java.util.jar.JarVerifier.update(Unknown Source)
        at java.util.jar.JarFile.initializeVerifier(Unknown Source)
        at java.util.jar.JarFile.getInputStream(Unknown Source)
        at sun.misc.URLClassPath$JarLoader$2.getInputStream(Unknown Source)
        at sun.misc.Resource.cachedInputStream(Unknown Source)
        at sun.misc.Resource.getByteBuffer(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

I solved this signature problem following this: "Invalid signature file" when attempting to run a .jar

回答1:

I'm assuming from your question that you have a file called jasperreports_extension.properties as a resource of your project, located inside src/main/resources. Because you have a dependency on jasperreports that also happens to have a resource called jasperreports_extension.properties at the root of the classpath, one is overwriting the other when you are making the jar-with-dependencies.

To tackle this problem, you should drop the maven-assembly-plugin and use maven-shade-plugin instead. This plugin provides out of the box a transformer that is able to merge two properties file together: by merging, I mean that one of the file is appended to the end of the other one:

Some jars contain additional resources (such as properties files) that have the same file name. To avoid overwriting, you can opt to merge them by appending their content into one file.

In this case, a sample configuration would be:

<plugin>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>jasperreports_extension.properties</resource>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.test.sample.MainClass</mainClass>
          </transformer>
        </transformers>
        <createDependencyReducedPom>false</createDependencyReducedPom>
      </configuration>
    </execution>
  </executions>
</plugin>

This will produce a final fat JAR where the content of jasperreports_extension.properties will be the content of your file and the file coming from the dependency.

Also, it will also have a correct MANIFEST since we specified to the plugin which class was the main class.


As a side note, when you are making fat JAR, the maven-assembly-plugin only provides basic support. From their homepage:

If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.