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:


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:


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:


Add lib* directory into result folder.

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


Now my maven-jar-plugin configuretion looks like below:

Copy dependencies to lib folder.

In package stage you can copy necessary dependencies to lib folder with maven-dependency-plugin:

In this code below instruction
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.


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-
│   └── 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:


* - 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