Tuesday, October 14, 2014

JasperReport and Maven for creating distributive

In previous articles we created some projects with reports which generated with JasperReport. It's time to create distributive and give it to end user. Here I told about running JasperReport reports from java code and used maven to compile and  run it. Here I will use maven for perform next tasks:
  • Clear unnecessary generated  *.jasper files.
  • Create result folder with result jar file, report templates and all necessary dependencies, but exclude extra dependencies such as junit.
But the first thing I advice set project encoding to utf-8. In project section of pom.xml write next:

<project>
  ...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  ...
</project>

Cleaning unnecessary files.

Templates of report stored in root of report directory. After compilation generated jasper and pdf files puts into jaspers folder :

├── pom.xml
├── reports
│   ├── jaspers
│   │   ├── java_beans_datasource_report.jasper
│   │   └── java_beans_datasource_report_subreport1.jasper
│   ├── java_beans_datasource_report.jrxml
│   └── java_beans_datasource_report_subreport1.jrxml

For remove jaspers directory with all content with mvn clean command add next code to build section of pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-clean-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <filesets>
      <fileset>
        <directory>reports</directory>
        <includes>
          <directory>jaspers</directory>
          <include>**/*.pdf</include>
          <include>**/*.jasper</include>
        </includes>
        <excludes>
          <exclude>**/*.jrxml</exclude>
        </excludes>
        <followSymlinks>false</followSymlinks>
      </fileset>
    </filesets>
  </configuration>
</plugin>

Set output directory.

Now, I need directory for distributibe. Let it be result folder in maven generated target directory. pom.xml contain configuration for maven-jar-plugin. Add to configuration section of it next code:

<outputDirectory>${project.build.directory}/result</outputDirectory>

Add lib* directory into result folder.

In maven-jar-plugin configuration/archive/manifest add next strings:

<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>

Now my maven-jar-plugin configuretion looks like below:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>com.blogspot.mkazarian.Demo</mainClass>
      </manifest>
    </archive>
    <outputDirectory>${project.build.directory}/result</outputDirectory>
  </configuration>
</plugin>

Copy dependencies to lib folder.

In package stage you can copy necessary dependencies to lib folder with maven-dependency-plugin:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.1</version>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/result/lib</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
        <includeScope>runtime</includeScope>
      </configuration>
    </execution>
  </executions>
</plugin>

In this code below instruction
<includeScope>runtime</includeScope>
allow include to lib folder only necessary for correct work dependencies.

Copy reports templates to reports folder.

You can do it with maven-resources-plugin.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/result/reports</outputDirectory>
        <resources>          
          <resource>
            <directory>reports</directory>
            <filtering>true</filtering>
          </resource>
        </resources>              
      </configuration>            
    </execution>
  </executions>
</plugin>

Result.
After run
mvn clean package
I obtain next result:
├── ireport-experiments-1.0-SNAPSHOT.jar
├── lib
│   ├── bcmail-jdk14-1.38.jar
│   ├── bcmail-jdk14-138.jar
│   ├── bcprov-jdk14-1.38.jar
│   ├── bcprov-jdk14-138.jar
│   ├── bctsp-jdk14-1.38.jar
│   ├── castor-1.2.jar
│   ├── commons-beanutils-1.8.0.jar
│   ├── commons-collections-3.2.1.jar
│   ├── commons-digester-2.1.jar
│   ├── commons-logging-1.1.1.jar
│   ├── groovy-all-2.3.6.jar
│   ├── itext-2.1.7.js2.jar
│   ├── jackson-annotations-2.1.4.jar
│   ├── jackson-core-2.1.4.jar
│   ├── jackson-databind-2.1.4.jar
│   ├── jakarta-regexp-1.4.jar
│   ├── jasperreports-5.6.0.jar
│   ├── jcommon-1.0.15.jar
│   ├── jdtcore-3.1.0.jar
│   ├── jfreechart-1.0.12.jar
│   ├── lucene-analyzers-common-4.5.1.jar
│   ├── lucene-core-4.5.1.jar
│   ├── lucene-queries-4.5.1.jar
│   ├── lucene-queryparser-4.5.1.jar
│   ├── lucene-sandbox-4.5.1.jar
│   ├── olap4j-0.9.7.309-JS-3.jar
│   └── xml-apis-1.3.02.jar
└── reports
    ├── java_beans_datasource_report.jrxml
    └── java_beans_datasource_report_subreport1.jrxml

Source code:

You can download source code from  BitBucket.

Related posts:

Resources


* - Here I repeat NetBeans output structure for compatibility with my current projects but it demonstrate way to create your own result struture .

No comments:

Post a Comment