Reputation: 16035
I'm starting to use profiles with Maven in order to build multi-environment jars.
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 (i.e.: only changing a properties file for each 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. This is: my app gets the DAO class from the config.properties file at runtime. 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).
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. 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.
Now, I changed my config.properties to 2 files: config-dev.properties and config-prod.properties. And changed the original pom.xml to use profiles like this:
<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. 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.
My formal questions:
Upvotes: 0
Views: 2154
Reputation: 9069
To active the profile via Netbeans, you may try the following task: -
properties
from the context menu.Configurations
from Categories
on the left panel.Activate
them by selecting them and click Activate
button or you can create the new on by clicking the Add
button.I hope this may help.
Upvotes: 1