【问题标题】:src/main/resources file overwritten by src/test/resourcessrc/main/resources 文件被 src/test/resources 覆盖
【发布时间】:2015-03-22 23:39:50
【问题描述】:

我使用 spring-boot 实现了一个 Web 服务。它在初始化期间从文件中读取。所以我将文件创建为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) 时,服务总是从测试资源中读取虚拟文件。

如何确保 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>

我也尝试使用&lt;excludes&gt; 排除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)

【问题讨论】:

  • 尼克。这很奇怪,你能多发布一点你的 pom.xml 吗?当您使用 mvn clean package 时,哪个版本在 jar 内结束?
  • 也将你的命令行发布到mvn exec:java,可能包括-Dexec.classpathScope="test"什么的。
  • 生成的war打包了正确的文件。
  • 您可以使用 -X 选项执行该命令以打开调试日志并以某种方式共享结果吗?
  • 我还注意到 pom 中的以下配置: org.codehaus.mojoexec-maven-plugin com.mycom.comnt.TestMaintest 看起来可以吗?类路径范围是test

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


【解决方案1】:

Exec 让你指定classpath scope

可以设置为compile,test,runtimesystem(默认为runtime)。

OP 设置为测试:

<classpathScope>test</classpathScope> 

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

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

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 2015-02-26
    • 2021-04-23
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2014-09-21
    相关资源
    最近更新 更多