我目前正在使用maven surefire插件对我的java源代码运行测试 . 但是,我遇到了一个问题,并希望得到一些关于如何解决它的建议 . 我目前有3个构建配置文件(local,int,prod),每个配置文件都有一组不同的数据库参数(url / username / password) . 问题是当我构建我的代码时,单元测试针对配置到每个构建配置文件的数据库运行,并且我希望单元测试仅针对本地数据库运行,无论我选择哪个构建配置文件 . 我已附上我的POM文件供您参考 .

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springshot</groupId>
    <artifactId>webservices</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Springshot Web Services</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>1.0.3-dse</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium.client-drivers</groupId>
            <artifactId>selenium-java-client-driver</artifactId>
            <version>1.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
    		<groupId>com.stackify</groupId>
    		<artifactId>stackify-metrics</artifactId>
    		<version>1.1.2</version>
		</dependency>
    </dependencies>

    <build>
        <finalName>ws</finalName>
        
        <plugins>
            <plugin>
            	<groupId>org.codehaus.mojo</groupId>
            	<artifactId>properties-maven-plugin</artifactId>
            	<version>1.0-alpha-2</version>
            	<executions>
            		<execution>
            			<phase>validate</phase>
            			<goals>
            				<goal>read-project-properties</goal>
            			</goals>
            			<configuration>
            				<files>
            					<file>src/main/filters/filter-${env}.properties</file>
            				</files>
            			</configuration>
            		</execution>
            	</executions>
            </plugin>
            
            <plugin>
        		<groupId>org.apache.maven.plugins</groupId>
        		<artifactId>maven-antrun-plugin</artifactId>
        		<version>1.6</version>
        		<executions>
        			<execution>
        				<phase>process-resources</phase>
        				<configuration>
        					<target>
        						<!--echo>${project.build.directory} - ${db.url} - ${db.username} - ${db.password}</echo-->
        						<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@db.url@" value="${db.url}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@db.username@" value="${db.username}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@db.password@" value="${db.password}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@alaska.username@" value="${alaska.username}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@alaska.password@" value="${alaska.password}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@delta.username@" value="${delta.username}"/>
                  				<replace file="${project.build.directory}/classes/CXFServlet-servlet.xml" token="@delta.password@" value="${delta.password}"/>
                  				<replace file="${project.build.directory}/classes/stackify-api.properties" token="@Stackify_ENV@" value="${stackify.env}"/>
                  			</target>
                  		</configuration>
                  		<goals>
                  			<goal>run</goal>
                  		</goals>
                  	</execution>
                </executions>
        	</plugin>
            
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <packagingExcludes>**/testclient/*.class</packagingExcludes>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${basedir}/src/main/java</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/config/AviationFeedApi.wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>com.springshot.datafeed.alaska.gen</extraarg>
                                    </extraargs>
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/config/alaska_binding.xml</bindingFile>
                                    </bindingFiles>
                                </wsdlOption>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/config/DeltaFeedApi.wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>com.springshot.datafeed.delta.gen</extraarg>
                                        <extraarg>-verbose</extraarg>
                                    </extraargs>
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/config/delta_binding.xml</bindingFile>
                                    </bindingFiles>
                                </wsdlOption>

                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                	<execution>
                		<id>hbm2java</id>
                		<phase>generate-sources</phase>
                		<goals>
                			<goal>hbm2java</goal>
                		</goals>
                		<configuration>
                    		<components>
                        		<component>
		                            <name>hbm2java</name>
		                            <implementation>jdbcconfiguration</implementation>
		                            <outputDirectory>src/main/java</outputDirectory>
                        		</component>
                    		</components>
                    		<componentProperties>
		                        <revengfile>src/main/config/reveng.xml</revengfile>
		                        <propertyfile>src/main/config/hibernate.properties</propertyfile>
		                        <packagename>com.springshot.model.sfs.gen</packagename>
		                        <jdk5>true</jdk5>
		                        <ejb3>true</ejb3>
                    		</componentProperties>
                		</configuration>
                	</execution>
                </executions>
                
                <dependencies>
                    <dependency>
                        <groupId>cglib</groupId>
                        <artifactId>cglib-nodep</artifactId>
                        <version>2.2.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.connector.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            
            
            <!-- Unit Test && Integration Test Plugins -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.0.RC0</version>
                <configuration>
                	<scanIntervalSeconds>5</scanIntervalSeconds>
                	<stopPort>9966</stopPort>
                	<stopKey>foo</stopKey>
                	<jvmArgs>-XX:PermSize=256M -XX:MaxPermSize=512M</jvmArgs>
                	<jettyXml>src/main/resources/jetty.xml</jettyXml>
                </configuration>
                <executions>
                	<execution>
                		<id>start-jetty</id>
                		<phase>pre-integration-test</phase>
                		<goals>
                			<goal>start</goal>
                		</goals>
                		<configuration>
                			<daemon>true</daemon>
                		</configuration>
                	</execution>
                	<execution>
                		<id>stop-jetty</id>
                		<phase>post-integration-test</phase>
                		<goals>
                			<goal>stop</goal>
                		</goals>
                	</execution>
                </executions>
            </plugin>
            <!-- Commented out because we currently don't need it, but leaving in for possible future use-->
            <!--plugin>
	            <groupId>org.codehaus.mojo</groupId>
	            <artifactId>selenium-maven-plugin</artifactId>
	            <version>2.3</version>
            	<executions>
               		<execution>
                  		<id>start</id>
	                  	<phase>pre-integration-test</phase>
	                  	<goals>
	                    	 <goal>start-server</goal>
	                  	</goals>
	                  	<configuration>
	                    	<background>true</background>
	                     	<logOutput>true</logOutput>
	                     	<multiWindow>true</multiWindow>
	                  	</configuration>
	               	</execution>
	               	<execution>
	                	<id>stop</id>
	                  	<phase>post-integration-test</phase>
	                  	<goals>
	                    	<goal>stop-server</goal>
	                  	</goals>
	               </execution>
            	</executions>
         	</plugin-->
	       	<plugin>
	        	<groupId>org.apache.maven.plugins</groupId>
	            <artifactId>maven-surefire-plugin</artifactId>
	            <version>2.18.1</version>
	            <configuration>
	            	<excludes>
	                	<!-- This does not run the integration tests during the unit test phase -->
	                	<exclude>**/integration-test/*Test.java</exclude>
	               </excludes>
	            </configuration>
	            <executions>
	               <execution>
	                  <id>integration-tests</id>
	                  <phase>integration-test</phase>
	                  <goals>
	                     <goal>test</goal>
	                  </goals>
	                  <configuration>
	                     <skip>false</skip>
	                     <excludes>
	                        <exclude>none</exclude>
	                     </excludes>
	                     <includes>
	                        <include>**/integration-test/*Test.java</include>
	                     </includes>
	                  </configuration>
	               </execution>
	            </executions>
	         </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>local</env>
            </properties>
        </profile>
        <profile>
            <id>int</id>
            <properties>
                <env>int</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

    <properties>
        <cxf.version>3.0.0</cxf.version>
        <spring.version>3.2.6.RELEASE</spring.version>
        <spring.security.version>3.2.5.RELEASE</spring.security.version>
        <hibernate.version>4.2.6.Final</hibernate.version>
        <mysql.connector.version>5.1.34</mysql.connector.version>
        <jetty.version>9.2.8-SNAPSHOT</jetty.version>
        <h2.version>1.3.164</h2.version>
    </properties>
</project>