[英]Maven Profiles not working and failing tests
I'm starting to use profiles with Maven in order to build multi-environment jars. 我开始将配置文件与Maven一起使用,以构建多环境的jar。
I followed the official docs to do this. 我按照官方文档进行操作。
First, validation question: 首先,验证问题:
I have read that you should always have one package generated by Maven project, but I only want to generate multi-environment jars (ie: only changing a properties file for each jar ). 我读过您应该始终有一个Maven项目生成的包,但是我只想生成多环境的jar(即: 仅更改每个jar的属性文件 )。 I don't consider necessary to generate multiple projects to do this, am I right?
我认为不必为此生成多个项目,对吗?
Now the explanation: 现在说明:
I have an app that reads a file and applies a certain set of reviews before going to insert some of the information to the database. 我有一个应用程序,该应用程序在将一些信息插入数据库之前会读取文件并应用一组特定的评论。 I want to test that this validations are ok, and that I get correct results whether or not it fails later at the database.
我想测试这种验证是否可行,并且无论以后是否在数据库中失败,我都会得到正确的结果。 So in this app I use a dynamically set DAO .
因此,在此应用中,我使用了动态设置的DAO 。 This is: my app gets the DAO class from the config.properties file at runtime.
这是:我的应用程序在运行时从config.properties文件获取DAO类。 I created some facade DAOs that will simulate the real DAO (for example: DAOApproveAll, that will simulate that all the transactions in the database went ok).
我创建了一些外观DAO,它们将模拟真实的DAO(例如:DAOApproveAll,它将模拟数据库中的所有事务都正常)。
In the unit testing I load config.properties to change (and later revert the change) of the value of the param daoimplclass that is the one that holds the class. 在单元测试中,我加载config.properties来更改(然后恢复更改)参数daoimplclass的值,该参数是保存该类的值。 For example:
例如:
Properties prop = Configurator.getProperties("config");
final String DAODEFAULT = prop.getProperty("daoimplclass");
final static String DAOAPPROVEALL = "com.package.dao.DAOAllApproved";
public void testAllAproved() {
try {
Processor processor = Processor.getInstance();
prop.setProperty("daoimplclass", DAOAPPROVEALL);
...
}
finally{prop.setProperty("daoimplclass", DAODEFAULT);}
I do many tests (using different DAO facades) in order to validate what would happen if different results occur in the database. 我进行了许多测试(使用不同的DAO外观),以验证如果数据库中出现不同的结果会发生什么。
Now, I changed my config.properties to 2 files: config-dev.properties and config-prod.properties . 现在,我将config.properties更改为2个文件: config-dev.properties和config-prod.properties 。 And changed the original pom.xml to use profiles like this:
并将原始pom.xml更改为使用如下配置文件:
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/config.properties"/>
<copy file="src/main/resources/config-dev.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/config.properties"/>
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Now when I do "Clean and build" in Netbeans I get errors executing the tests because it can't find the config.properties . 现在,当我在Netbeans中执行“清理并构建”时,执行测试会出错,因为它找不到config.properties 。 Of course I create a third config.properties (the other two will be with the -dev and -prod) ant it compiles but doesn't generate 2 jars but only one.
当然,我创建了第三个config.properties (其他两个将与-dev和-prod一起使用),它可以编译但不会生成2个jar,而只能生成一个。
My formal questions: 我的正式问题:
To active the profile via Netbeans, you may try the following task: - 要通过Netbeans激活配置文件,您可以尝试以下任务:-
properties
from the context menu. properties
。 Configurations
from Categories
on the left panel. Categories
选择Configurations
。 Activate
them by selecting them and click Activate
button or you can create the new on by clicking the Add
button. Activate
按钮来Activate
它们,也可以通过单击“ Add
按钮来创建新对象。 I hope this may help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.