准备工作:
将 FR9.0 的 WebReport 目录拷贝到 finereport-maven 文件夹中。
Maven 添加阿里云镜像
alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
集成步骤
零、下载 demo 项目
[download id="1708"]
一、了解 demo 项目的结构
明确工程的目录结构,后续步骤的脚本或代码中会使用相对路径。目录树:
. ├── SpringFRdemo // 业务工程(Spring Boot 工程) │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ ├── src │ ├── main │ └── test └── finereport-maven // 包含帆软相关文件和 Maven 配置,不用导入 IDE 中 ├── install_fr_lib_to_local_maven_repo.bat // 一键安装脚本(Win) ├── install_fr_lib_to_local_maven_repo.sh // 一键安装脚本(Mac) ├── pom.xml // 管理 FR 的 Maven 依赖 └── WebReport ├── ... └── WEB-INF // 包含 jar 包、模版、日志、finedb 等文件
二、安装 finereport-maven 到本地 Maven 仓库
将 FR 的相关 jar 包封装为一个本地 Maven 库,方便业务工程添加依赖。
Mac 下
命令行进入 finereport-maven 目录,运行 install_fr_lib_to_local_maven_repo.sh
Win 下
双击 install_fr_lib_to_local_maven_repo.bat 看到提示“开始使用吧”,且过程中没有报错,则转入下一步骤。
(以下步骤在业务项目中操作,此例中为 SpringFRdemo)
三、用 IDEA 打开 SpringFRdemo/pom.xml
选择 open as project。
四、pom 中加入 finereport-maven 的依赖
<!-- 依赖帆软的jar包 -->
<dependency>
<groupId>com.fr</groupId>
<artifactId>finereport-maven</artifactId>
<version>9.0</version>
</dependency>
五、在 Controller 中添加帆软 Servlet 的映射
@RestController @SpringBootApplication public class DemoApplication { // 其他业务代码...
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new ReportServlet(),"/ReportServer");
}
}
六、增加一个 Configuration 类,修改内嵌 Tomcat 的工作路径
/**
- 内嵌Tomcat 运行项目目录 */ @Configuration public class DocumentDirectoryCustomizer implements EmbeddedServletContainerCustomizer { public void customize(ConfigurableEmbeddedServletContainer container) { //项目目录 container.setDocumentRoot(new File("../finereport-maven/WebReport")); } }
说明
将 Tomcat 的工作目录切换到包含帆软 WEB-INF 的目录下。如果不指定的话,内嵌 Tomcat 会自己找一个临时文件夹作为工作目录,加载不到报表项目需要的各种文件(模版、日志、数据库等等)。
七、运行测试
在 IDEA 中运行 DemoApplication,访问 localhost:8080/ReportServer,测试相关功能是否正常。
八、生成可运行 jar 包
因为使用的是 Spring Boot,确保业务工程的 pom 中有如下配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在业务工程目录下(SpringFRdemo/),执行 mvn package。发现在 target 目录下生成一个 jar 包:demo-0.0.1-SNAPSHOT.jar。
九、运行 jar 包
因为我们在代码中使用了相对路径,所以 jar 包的位置也要注意(否则相对路径不生效了)。把上一步生成的 jar 包移动到 published-demo 目录下。目录树:
. ├── SpringFRdemo │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ └── target ├── finereport-maven │ ├── WebReport │ ├── install_fr_lib_to_local_maven_repo.bat │ ├── install_fr_lib_to_local_maven_repo.sh │ └── pom.xml ├── published-demo └── demo-0.0.1-SNAPSHOT.jar
命令行进入 published-demo 目录,执行:
java -jar demo-0.0.1-SNAPSHOT.jar
浏览器访问 localhost:8080/ReportServer。
十、额外说明(可略过)
跑通以上步骤之后,可以根据实际情况调整。例如:
- 把相对路径改为绝对路径
- 去掉 finereport-maven 中不需要的依赖
- 自定义目录结构
- 修改脚本
- 去掉 WebReport 中不需要的文件
- ……