读取属性文件 "inStream parameter is null"

Reading properties files "inStream parameter is null"

我之前阅读过 属性 个文件,我意识到有很多关于该主题的链接,但我认为我的属性文件由于插件的原因而未正确打包。我的 Eclipse 项目在 src/main/resources 中有一个名为 environment.properties 的属性文件。我试过通过一个方法来读取它,甚至试图在不同的时间将方法声明为静态和非静态的:

  static Properties properties = new Properties();

  public void getClassPathProperties() {
      
      LOGGER.debug("getClassPathProperties Enter");
      try (final InputStream input = Utils.class.getClassLoader().getResourceAsStream("environment.properties")) {
          properties.load(input);
          LOGGER.debug("properties found:");
          for (Object key : properties.keySet()) {
              LOGGER.debug("" + ((String) key) + "=" + properties.getProperty((String) key));             
          }
      } catch (Exception e) {
              LOGGER.error("Unable to find environment.properties on classpath to Utils.class");
              e.printStackTrace();
      }
      LOGGER.debug("getClassPathProperties Exit");
  } 

我先尝试了静态声明,但也尝试了非静态,因为一些帖子提到它在被声明为静态之前一直有效。我也曾尝试将 environment.properties 文件手动放置在不同位置的 JAR 文件中,但没有成功。在 Eclipse 中,例外是:

14:33:52.676 [main] ERROR com.goprecise.ams.handlers.utils.Utils - Unable to find environment.properties on classpath to Utils.class
java.lang.NullPointerException: inStream parameter is null
    at java.base/java.util.Objects.requireNonNull(Objects.java:246)
    at java.base/java.util.Properties.load(Properties.java:403)

并且从 Maven 命令行报告 Properties.load() 上的 NPE(LOGGER.error() 消息来自显示的代码)。 pom.xml 是:

<?xml version="1.0"?>
... header ...
  <properties>
    <version.org.kie>7.48.0.Final-redhat-00004</version.org.kie>
    <version.org.powermock>1.7.4</version.org.powermock>
    <version.junit>4.12</version.junit>
    <version.org.slf4j>1.7.26</version.org.slf4j>
    <java.module.name>${project.name}</java.module.name>
  </properties>

  <dependencyManagement>
    <dependencies>
... depeendencies 
    </dependencies>
  </dependencyManagement>

  <dependencies>
... many more dependencies
  </dependencies>
  <build>
    <sourceDirectory>${project.build.directory}/generated-sources/java</sourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
          <include>*.png</include>
        </includes>
      </resource>
      <resource>
        <directory>${project.build.directory}/maven-shared-archive-resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>*.part</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>templating-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <id>filter-src</id>
            <goals>
              <goal>filter-sources</goal>
            </goals>
            <configuration>
              <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
              <outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>copy-repository-resources</id>
            <phase>compile</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <encoding>UTF-8</encoding>
              <resources>
                <resource>
                  <directory>target/generated-sources/annotations</directory>
                  <includes>
                    <include>repoindex.html</include>
                    <include>*.wid</include>
                  </includes>
                  <filtering>true</filtering>
                </resource>
              </resources>
              <outputDirectory>target/classes</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <annotationProcessors>
            <annotationProcessor>org.jbpm.process.workitem.core.util.WidProcessor</annotationProcessor>
          </annotationProcessors>
          <compilerArgs>
            <arg>-AwidName=${project.artifactId}</arg>
            <arg>-AgenerateTemplates=true</arg>
            <arg>-AgenerateWids=true</arg>
            <arg>-AwidsResources=${project.artifactId}.wid:widtemplate.st</arg>
            <arg>-AtemplateResources=kie-deployment-descriptor.xml:kie-ddtemplate.st,serviceinfo.json:serviceinfo.st,repoindex.html:repoindex.part,index.html:indextemplate.st,${project.artifactId}.bpmn2:defaultprocess.st</arg>
          </compilerArgs>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <!-- root module has no assembly so ignore it -->
          <ignoreMissingDescriptor>true</ignoreMissingDescriptor>
          <descriptors>
            <descriptor>${project.basedir}/assembly/assembly.xml</descriptor>
          </descriptors>
          <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <appendAssemblyId>false</appendAssemblyId>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.2</version>
        <executions>
          <execution>
            <id>integration-test-execution</id>
            <phase>verify</phase>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <systemPropertyVariables>
            <groupId>${project.groupId}</groupId>
            <artifactId>${project.artifactId}</artifactId>
            <version>${project.version}</version>
            <builddir>${project.build.directory}</builddir>
          </systemPropertyVariables>
          <failIfNoTests>false</failIfNoTests>
          <test>${it.test}</test>
          <includes>
            <include>**/*IntegrationTest.java</include>
          </includes>
          <argLine>${failsafe.arg.line}</argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.commonjava.maven.plugins</groupId>
        <artifactId>project-sources-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>project-sources</id>
            <goals>
              <goal>archive</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
          <includes>
            <include>**/*Test.java</include>
          </includes>
          <excludes>
            <exclude>**/*IntegrationTest.java</exclude>
          </excludes>
          <argLine>-Xmx1024m -Dfile.encoding=UTF-8</argLine>
          <systemPropertyVariables>
            <apple.awt.UIElement>true</apple.awt.UIElement>
            <org.uberfire.nio.git.daemon.enabled>false</org.uberfire.nio.git.daemon.enabled>
            <org.uberfire.nio.git.ssh.enabled>false</org.uberfire.nio.git.ssh.enabled>
            <org.uberfire.sys.repo.monitor.disabled>true</org.uberfire.sys.repo.monitor.disabled>
          </systemPropertyVariables>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>default-jar</id>
          </execution>
          <!-- No OSGi manifestEntries for <goal>jar</goal>: if it supported, then felix has already added them -->
          <execution>
            <id>test-jar</id>
            <goals>
              <goal>test-jar</goal>
            </goals>
            <configuration>
              <skipIfEmpty>true</skipIfEmpty>
              <excludes>
                <exclude>**/logback-test.xml</exclude>
                <exclude>**/jndi.properties</exclude>
              </excludes>
              <archive>
                <manifestEntries>
                  <Bundle-SymbolicName>${java.module.name}.tests</Bundle-SymbolicName>
                  <Bundle-Version>
                    ${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}.${osgi.snapshot.qualifier}
                  </Bundle-Version>
                  <Bundle-Name>${project.name}</Bundle-Name>
                  <Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
                </manifestEntries>
              </archive>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <archive>
            <manifest>
              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-remote-resources-plugin</artifactId>
        <version>1.5</version>
        <configuration>
          <resourceBundles>
            <resourceBundle>org.jbpm.contrib:template-resources:${version.org.kie}</resourceBundle>
          </resourceBundles>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>process</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

我希望 JAR 中的代码在 运行 时间找到属性:如果 属性 文件不属于 src/main/resources(典型位置)它在哪里属于?如果插件干扰了这个,我如何配置插件以启用我的阅读属性(或者只是移动文件以使其能够被读取)?

所以 属性 文件也需要添加到 src/test/java 因为它是从测试用例中调用的。实际上在目标目录中创建了两个单独的 JAR:

ams-pam-workitemhandlers-2.0.1.1.jar
ams-pam-workitemhandlers-2.0.1.1-tests.jar

我认为应用程序会在 src/main/resources 中找到资源文件,否则我将不得不将其移出,但测试用例肯定会在 src/test/resources 中找到它。

是声明:

      <resource>
        <directory>src/main/resources</directory>
        ...
        <includes>
          <include>*.png</include>  <!-- just this one isn't enough -->
        </includes>
      </resource>

如果您(重新)明确声明资源,则它不会与默认值叠加,即默认值被覆盖。查看您的 target/classes,您会发现那里只有 PNG 图片。

resources:[testR|r]esources:

Always uses the project.build.[testR|r]esources element to specify the resources to copy.

POM Reference, Super POM

<project>
  ...  
  <build>
    ...
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>

这意味着:默认情况下从这些目录中获取 all,但如果覆盖则从这些目录中获取 nothing(除非您在覆盖时包含它).