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

标签 java web-services maven spring-boot pom.xml

我使用 spring-boot 实现了一个网络服务。它在初始化期间从文件中读取。所以我将文件创建为 src/main/resources/files/init_file.txt .这是一个巨大的文件,在初始化期间需要一些时间来读取。不管怎样,Web 服务能够从文件中读取并按预期工作。

然后我添加了单元测试。由于这个文件很大,我使用了一个我创建的轻量级虚拟文件 src/test/resources/files/init_file.txt .请注意,此虚拟文件位于 src/test/resources 中,而不是在 src/ma​​in/resources 中。

单元测试按预期工作。但是,现在当我运行该服务 ( mvn exec:java ) 时,该服务总是从测试资源中读取虚拟文件。

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

摘自 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)

最佳答案

Exec 让你指定 classpath scope .

可以设置为compile,test,runtime 或者system(默认为runtime )。

OP 将其设置为测试:

<classpathScope>test</classpathScope> 

要么删除它,要么从命令行指定类路径范围:

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

关于java - src/main/resources 文件被 src/test/resources 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118271/

相关文章:

java - Java 中的类可以克隆吗?

spring - 连接到本地主机 :5432 refused after docker run when port 5432 is clearly open and listening?

java - "Maven in 5 Minutes"教程中的原型(prototype)插件错误

java - 在rest服务java eclipse中没有得到json响应

java - 将数据从 ArrayList<T> 插入到 MySql DB

java - 创建 setX(x) 时遇到问题

java - 限制对 Amazon EC2 上的 Web 服务的访问

java - 如何使用 Postman 从curl 发布 REST

java - 如何下载apk并在android 6上安装

linux - 适用于 Linux 上 REST 和/或 SOAP Web 服务开发的堆栈/框架