首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >src/test/resources覆盖的src/main/resources文件

src/test/resources覆盖的src/main/resources文件
EN

Stack Overflow用户
提问于 2015-01-23 20:33:09
回答 1查看 2.7K关注 0票数 2

我用spring实现了一个web服务。它在初始化时从文件中读取。因此,我将该文件创建为src/main/resources/files/init_file.txt。这是一个巨大的文件,在初始化过程中需要一段时间才能读取。无论如何,web服务能够从文件中读取并按预期工作。

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

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

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

pom.xml摘录

代码语言:javascript
复制
<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/*,但它没有起作用。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2015-01-23 22:12:10

Exec允许您指定类路径范围

它可以设置为compiletestruntimesystem (默认为runtime)。

OP将其设置为测试:

代码语言:javascript
复制
<classpathScope>test</classpathScope> 

从命令行中移除它或指定类路径作用域:

代码语言:javascript
复制
mvn exec:java -Dexec.classpathScope="runtime"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28118271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档