3
votes

I am trying to use Jasper using the jasperreports-maven-plugin.

So you create your .jrxml in a directory src/main/jasperreports and the plugin compiles it and creates a .jasper file in target/jasper. So far this is working.

Now I would like to read this .jasper file into a Java class in order to fill the report. This is where my problem is. How do I access this .jasper file from target/jasper?

I thought I needed to add it as additional resource directory in my pom.xml:

  <build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>target/jasper</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jasperreports-maven-plugin</artifactId>
            <configuration>
              <outputDirectory>${project.build.directory}/jasper</outputDirectory>
            </configuration>
            <executions>
              <execution>
              <goals>
                  <goal>compile-reports</goal>
              </goals>
              </execution>
            </executions>
        ...

And then get it using getResource like this:

URL resource = getClass().getResource("/target/jasper/DataSourceReport.jasper");

However, this returns null. The file DataSourceReport.jasper is inside my project under target/jasper, but it seems to be unable to find it.

Can anyone tell me what I'm doing wrong or why this isn't working?

5

5 Answers

1
votes

Thanks. That was indeed what I overlooked. The resource element only made sure the folder is imported as resource in Eclipse but I needed to add another maven plugin to specify more than one resource folder to add to the jar that is created. I thought this was done by the resources element.

This is what I added to make it work:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.7</version>
      <executions>
        <execution>
          <id>add-source</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>add-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>${basedir}/src/main/resources</source>
              <source>${project.build.directory}/jasper</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
1
votes

You can use YourClassName.class.getResourceAsStream(jasperLocation ) it will return you input stream of the file and inputstream you can pass to JRLoader. It may help you.

0
votes

The question is more about how to load file from resource in Java, than Jasper plug-in etc.

The plugin do to job, what you need is to load the file (.jasper) properly to your application .

The resources depend of the jar you have created. So may it be that you skip that location with report definitions. Try to open, the jar file with and check that contain the reports.

Here is describe how to manage your resources in Java project.

0
votes

Open that .jasper file in ireport. There is multiple options in ireport just open that file then you will see one popup "Do you need to convert this file in to .jrxml?" click "Yes".

0
votes

In this way your jrxml will run each time whenever you run the application. My recommandation is to use JasperReports dependency to compile your jasper from classes repository. The way like

keep your jasper repository under classes path Load and compile it through the following code:

       //jasper is the repository under classses repo
       //ReceiveReport.jasper is the report file
       jasperStream = this.getClass().getResourceAsStream("/jasper/ReceiveReport.jasper");

Use this dependency

       <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.4.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.0.0</version>
        </dependency>