【问题标题】:Read .jasper file from target/jasper从 target/jasper 读取 .jasper 文件
【发布时间】:2018-12-31 13:17:52
【问题描述】:

我正在尝试通过 jasperreports-maven-plugin 使用 Jasper。

所以你在目录 src/main/jasperreports 中创建你的 .jrxml,插件编译它并在 target/jasper 中创建一个 .jasper 文件。 到目前为止,这是有效的。

现在我想将这个 .jasper 文件读入一个 Java 类以填充报告。这就是我的问题所在。如何从 target/jasper 访问这个 .jasper 文件?

我想我需要将它作为附加资源目录添加到我的 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>
        ...

然后像这样使用 getResource 获取它:

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

但是,这将返回 null。 文件DataSourceReport.jasper在我的target/jasper下的项目里面,但是好像找不到。

谁能告诉我我做错了什么或者为什么这不起作用?

【问题讨论】:

    标签: java maven resources jasper-reports


    【解决方案1】:

    谢谢。 这确实是我忽略的。资源元素仅确保文件夹在 Eclipse 中作为资源导入,但我需要添加另一个 maven 插件来指定多个资源文件夹以添加到创建的 jar 中。我认为这是由资源元素完成的。

    这是我添加使其工作的:

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

    【讨论】:

      【解决方案2】:

      您可以使用 YourClassName.class.getResourceAsStream(jasperLocation ) 它将返回文件的输入流和您可以传递给 JRLoader 的输入流。它可能会帮助你。

      【讨论】:

        【解决方案3】:

        问题更多是关于如何从Java中的资源加载文件,而不是Jasper插件等。

        插件要做的就是将文件(.jasper)正确加载到您的应用程序中。

        资源取决于您创建的 jar。因此,您可能会使用报告定义跳过该位置。尝试打开 jar 文件并检查是否包含报告。

        Here 描述了如何在 Java 项目中管理你的资源。

        【讨论】:

          【解决方案4】:

          在 ireport 中打开该 .jasper 文件。 ireport 中有多个选项,只需打开该文件,您将看到一个弹出窗口“您需要​​将此文件转换为 .jrxml 吗?”点击“是”。

          【讨论】:

            【解决方案5】:

            这样,您的 jrxml 将在您每次运行应用程序时运行。我的建议是使用 JasperReports 依赖项从类存储库中编译您的 jasper。像这样的方式

            将您的 jasper 存储库保存在类路径下 通过如下代码加载编译:

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

            使用这个依赖

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-10-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多