src/main/resources 文件被 src/test/resources 覆盖

问题描述 投票:0回答:2

我使用 spring-boot 实现了一个 Web 服务。它在初始化期间从文件中读取。所以我将文件创建为

src/main/resources/files/init_file.txt
。这是一个巨大的文件,在初始化过程中需要一些时间来读取。不管怎样,网络服务能够读取文件并按预期工作。

然后我添加了单元测试。由于这个文件很大,我使用了一个轻量级的虚拟文件,我将其创建为

src/test/resources/files/init_file.txt
。请注意,这个虚拟文件位于 src/test/resources 中,而不是在 src/main/resources 中。

单元测试按预期工作。但是,现在当我运行服务时 (

mvn exec:java
),该服务总是从测试资源中读取虚拟文件。

如何确保 Web 服务读取正确的文件?

摘自

pom.xml

<modelVersion>4.0.0</modelVersion>
<artifactId>myproj-main</artifactId>
<name>myproj : Main</name>
<packaging>war</packaging>



<build>
<resources>
  <resource>
    <directory>src/main/resources/</directory>
    <excludes>
      <exclude>version.properties</exclude>
      <exclude>conf/**</exclude>
    </excludes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources/</directory>
    <includes>
      <include>version.properties</include>
    </includes>
    <filtering>true</filtering>
  </resource>

  <resource>
    <directory>src/main/config</directory>
    <includes>
      <include>static.properties</include>
    </includes>
    <filtering>true</filtering>
  </resource>

  <resource>
    <directory>src/main/config/local</directory>
    <includes>
      <include>application.properties</include>
      <include>secure.properties</include>
    </includes>
    <filtering>true</filtering>
  </resource>

  <resource>
    <directory>target/generated-sources/xmlbeans/resources</directory>
  </resource>
</resources>

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
  </plugin>
  <!-- Springboot boot for maven -->
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <layout>ZIP</layout>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <!-- Sets the VM argument line used when unit tests are run. -->
      <argLine>${surefireArgLine}</argLine>
      <excludes>
        <exclude>**/IT*.java</exclude>
      </excludes>
    </configuration>
  </plugin>
  <!--
       <plugin>
                  <groupId>org.jacoco</groupId>
                  <artifactId>jacoco-maven-plugin</artifactId>
                  <configuration>
          <propertyName>surefireArgLine</propertyName>
                      <destFile>${jacoco.out.path}${jacoco.out.file}</destFile>
                  </configuration>
                  <executions>
                      <execution>
                          <id>PRE-TEST-PARENT</id>
        <goals>
                              <goal>prepare-agent</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>
  -->
  <!-- Copy application configurations to test resources for tests. -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>process-test-resources</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target>
            <!-- Include environment-invariant content from src/main/config -->
            <copy overwrite="true" todir="${project.build.testOutputDirectory}" verbose="true">
              <fileset dir="${project.basedir}/src/main/config" includes="*">
                <type type="file"/>
                <!-- don't include subfolders -->
              </fileset>
            </copy>

            <!-- Include environment-variant content from src/main/config/${test.config.profile}, including subfolders -->
            <copy overwrite="true" todir="${project.build.testOutputDirectory}" verbose="true">
              <fileset dir="${project.basedir}/src/main/config/${test.config.profile}" includes="**/*"/>
            </copy>
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>

我也尝试使用

<excludes>
来排除
src/test/resources/*
但没有成功。

nik@ubuntu:myproj-main$ mvn exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myproj : Main 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ myproj-main >>>
[INFO] 
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce) @ myproj-main ---
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ myproj-main <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ myproj-main ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.0.2.RELEASE)

2015-01-23 13:05:43,101  INFO [com.mycom.comnt.Application] Starting Application on ubuntu with PID 63446 (/home/nik/work/myproj/myproj-main/target/classes started by nik in /home/nik/work/myproj/myproj-main)
2015-01-23 13:05:43,101 DEBUG [com.mycom.comnt.Application] Running with Spring Boot v1.0.2.RELEASE, Spring v4.0.5.RELEASE
2015-01-23 13:05:48,311  INFO [com.mycom.comnt.Application] ServletContext initialized
2015-01-23 13:05:48,496  INFO [com.mycom.comnt.services.EventStatusService] Event availability service: found 3 available events
2015-01-23 13:05:51,822  INFO [com.mycom.comnt.Application] Started Application in 8.896 seconds (JVM running for 11.973)
java web-services maven spring-boot pom.xml
2个回答
1
投票

Exec 让您指定 类路径范围

可以设置为

compile
test
runtime
system
(默认为
runtime
)。

OP将其设置为测试:

<classpathScope>test</classpathScope> 

删除它或从命令行指定类路径范围:

mvn exec:java -Dexec.classpathScope="runtime"

0
投票

仅用于使用应用程序上下文进行测试的解决方案(用@SpringBootTest注释)。

您好,也许这个问题的答案已经太晚了,但是对于那些正在寻找这个问题的人,我将留下我找到的解决方案。这个解决方案非常简单。

使用主 prop 文件覆盖配置“spring.config.location”。

@SpringBootTest(properties = {"spring.config.location=src/main/resources/application.yml"})
© www.soinside.com 2019 - 2024. All rights reserved.