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

时间:2015-01-23 20:33:10

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

我使用spring-boot实现了一个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

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

1 个答案:

答案 0 :(得分:1)

Exec允许您指定classpath scope

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

OP将其设置为测试:

<classpathScope>test</classpathScope> 

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

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