<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
明确工程的目录结构,后续步骤的脚本或代码中会使用相对路径。
目录树:
.
├── 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 等文件
将 FR 的相关 jar 包封装为一个本地 Maven 库,方便业务工程添加依赖。
命令行进入 finereport-maven 目录,运行 install_fr_lib_to_local_maven_repo.sh
双击 install_fr_lib_to_local_maven_repo.bat
看到提示“开始使用吧”,且过程中没有报错,则转入下一步骤。
(以下步骤在业务项目中操作,此例中为 SpringFRdemo)
选择 open as project。
<!-- 依赖帆软的jar包 -->
<dependency>
<groupId>com.fr</groupId>
<artifactId>finereport-maven</artifactId>
<version>9.0</version>
</dependency>
@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");
}
}
/**
* 内嵌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,测试相关功能是否正常。
因为使用的是 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 包移动到 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
。
跑通以上步骤之后,可以根据实际情况调整。例如: