我有一个Maven多模块/配置文件项目,其运行测试模块时显然没有应用测试资源。这些资源定义了应用程序将使用哪个数据库(一个是我的create.sql脚本文件):
DROP DATABASE IF EXISTS "${db.name}";
CREATE DATABASE "${db.name}"
WITH TEMPLATE = template0
OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
&#39>项目'结构是:
project/ pom.xml //packing: pom
|
+-- core/ pom.xml //packing: war
|
+-- module-a/ pom.xml //packing: jar
|
+-- module-b/ pom.xml //packing: jar
|
+-- test/ pom.xml //packing: jar
+-- ** src/test/resources/create.sql **
根源'项目' pom定义为:
<?xml ... maven-4.0.0.xsd">
<!-- ... -->
<packaging>pom</packaging>
<properties>
<!-- global properties -->
</properties>
<modules>
<module>core</module>
<module>module-a</module>
<module>module-b</module>
<module>test</module> <!-- CAN BE REMOVED -->
</modules>
<repositories>
<!-- some repo declarations -->
</repositories>
<dependencies>
<!-- all general dependencies -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
&#39;核心&#39;战争应用本身就是定义:
<?xml ... maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- 'project' is the parent -->
</parent>
<!-- ... -->
<packaging>war</packaging>
<profiles>
<profile>
<id>dev-a</id>
<properties>
<profileName>dev-a</profileName>
**<db.name>db</db.name>**
<skipTests>true</skipTests>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>dev-b</id>
<properties>
<profileName>dev-b</profileName>
**<db.name>db</db.name>**
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>module-b</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>test-a</id>
<properties>
<profileName>test-a</profileName>
**<db.name>db_test</db.name>**
<selenium.context>http://127.0.0.1:9999/${profileName}</selenium.context>
</properties>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>test-b</id>
<properties>
<profileName>test-b</profileName>
**<db.name>db_test</db.name>**
<selenium.context>http://127.0.0.1:9999/${profileName}</selenium.context>
</properties>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>module-b</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<finalName>${profileName}##${app.version}.${maven.build.timestamp}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<attachClasses>true</attachClasses>
<failOnMissingWebXml>false</failOnMissingWebXml>
<useCache>false</useCache>
<archive>
<manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>${project.artifactId}#${maven.build.timestamp}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</testResource>
</testResources>
</build>
模块定义非常简单且类似(模块-b被压缩):
<?xml ... maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- 'project' is the parent -->
</parent>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
最后是我的测试模块,一个封装了一些黄瓜测试和sql脚本文件的jar模块:
<?xml ... maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- 'project' is the parent -->
</parent>
<!-- ... -->
<packaging>jar</packaging>
<dependencies>
<!-- All test dependencies + ... -->
<dependency>
<groupId>...</groupId>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>...</groupId>
<artifactId>module-b</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>...</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>classes</classifier>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skipTests>true</skipTests>
</configuration>
</execution>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>Tomcat 7</server>
<path>/${project.artifactId}</path>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war-only</goal>
</goals>
<configuration>
<fork>true</fork>
<port>9999</port>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>start-selenium</id>
<phase>pre-integration-test</phase>
<configuration>
<target>
<exec executable="cmd.exe" spawn="true">
<arg value="/u/c" />
<arg value="${basedir}/src/test/resources/start-selenium.bat" />
<arg line="${basedir}/target/${project.build.finalName}/WEB-INF/lib/selenium-server-standalone-${selenium.version}.jar" />
<arg line="-timeout 30 -port 4444" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用这种结构,当我运行dev-a或dev-b时,我得到两个不同的war文件,所有属性都被正确替换,但是当我执行test-a或test-b时,我的测试模块中的资源不是受测试配置文件中的属性影响,$ {db.name}保持不变
所以我尝试了另一种方法将测试配置文件从核心移动到测试模块,并且使用正确的数据库名称(db_test)对测试模块进行替换,但作为核心战争模块的缺点。 t(保留为db)。
我很容易注意到我的结构是错误的,但要点是:需要进行哪些修改才能使用相同的键属性为war和test-jar文件创建正确的配置文件(dev | test)?我应该如何重新声明模块及其层次结构?上述,我试图实现:
runnig dev-a: A war file using db + module-a.jar
runnig dev-b: A war file using db + module-b.jar
runnig test-a: A war file using db_test + module-a.jar + test-jar using the same database db_test
runnig test-b: A war file using db_test + module-b.jar + test-jar using the same database db_test
最诚挚的问候。
答案 0 :(得分:0)
我已经在我认为理想的方法(WITH测试模块)和实用的解决方案(生成构建或执行集成测试的maven任务)之间实现了部分解决方案。
所以我将测试类和资源从测试模块移到了war模块,并删除了父pom中的模块测试。