Maven: pass the build even if one module fails - java

I am trying to generate an aggregate Jacoco code coverage report by following the steps here.
My initial maven project may contain multiple modules and I add an aggregator module aggregator to the main pom dynamically through my code.
The folder structure looks somewhat like this (there can be any number of modules and sub modules in the main project):
/SampleProject/
-- pom.xml
-- module1/
---- pom.xml
---- src/
-- module2/
---- pom.xml
---- src/
-- module3/
---- pom.xml
---- src/
-- aggregator/
---- pom.xml
This is the main pom:
SampleProject/pom.xml . (here, the aggregator module is added dynamically through my code)
<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.sample</groupId>
<artifactId>sampleProject</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>sampleProject</name>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>aggregator</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Each module has its own pom.xml. and the aggregator pom (which is generated by my code) looks something like this (each module is added as a dependency in this pom. This information is fetched from the individual poms of the modules):
aggregator/pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sample</groupId>
<artifactId>sampleProject</artifactId>
<version>1.0</version>
</parent>
<artifactId>aggregator</artifactId>
<dependencies>
<dependency>
<groupId>com.sample.module1</groupId>
<artifactId>module1</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.sample.module2</groupId>
<artifactId>module2</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.sample.module3</groupId>
<artifactId>module3</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${argLine} -Xms256m -Xmx2048m</argLine>
<forkCount>1</forkCount>
<runOrder>random</runOrder>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and, to generate the aggregate report I use the following command:
mvn clean test -DskipTests=false
This generates the aggregate report in 'aggregator/target/site/jacoco-aggregate/index.html'
Now, the problem is if something goes wrong with only the aggregator build, and that build fails (but all other modules' build is successful). Then the maven output will look something like this:
[INFO] module1 .......................................... SUCCESS [ 2.197 s]
[INFO] module2 .......................................... SUCCESS [ 11.287 s]
[INFO] module3 .......................................... SUCCESS [ 6.969 s]
[INFO] aggregator ....................................... FAILURE [ 1.241 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 00:21 min
[INFO] Finished at: 2018-07-26T13:01:12-07:00
[INFO] Final Memory: 258M/1149M
Which means that the overall build is also marked as failed if just the aggregator part fails. This causes problem in the next step in the deployment pipeline.
So, what I want is that even if the aggregator build is failing, the overall build should be marked as passed (i.e. ignore the failing aggregator build) so that I can proceed with the next step in deployment, so that not generating the code coverage report, does not fail my entire deployment process. (I will handle the missing code coverage report later in the process).
Is there some way to achieve this? any help would be appreciated.
UPDATE 1:
I already check this answer and this is not what I want. Using -fae flag in maven would still mark the build as failed at the end.
Also, I do not want the -fn flag either because then I would be ignoring actual build failures in the modules as well, which is not desired.
What I'm looking for is a way to ignore the failures of just one module in maven.
I'm not really sure if doing that is possible, any alternate suggestions are welcome too.
UPDATE 2:
Update and some background info.:
So, I am building this tool as kind of a central tool which runs on all projects so it should be generic enough to cater to different types of configurations, without changing anything in the main project (on which the tests are being run).
As of now, it seems what I am looking for is not possible. So, as a workaround, I am asking the main project's owner to include a config file which tells my tool which packages to be included for code coverage.
Will update here if I find a better solution or workaround.

Related

PMD fails with custom rulesets for project with multiple modules

How to define custom-rulesets.xml in parent project, so it is re-used in children modules?
I have a PMD example project that works well (you can check later).
However, this maven project does not have children.
Project structure
pmd-java-14-example _.
|_ core
|_ tasks
custom-rulesets.xml
<?xml version="1.0"?>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#shortvariable -->
<ruleset
name="custom-ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>
<rule ref="category/java/design.xml/SimplifiedTernary"/>
<rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal"/>
<rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal"/>
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
<rule ref="category/java/codestyle.xml/DuplicateImports"/>
<rule ref="category/java/codestyle.xml/ShortMethodName"/>
<rule ref="category/java/codestyle.xml/ShortVariable"/>
</ruleset>
When I have a parent pom and a child module, pmd fails for the child module, because it cannot find custom-rulesets.xml.
pom.xml (parent pom that has a child module).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yk.utils</groupId>
<artifactId>pmd-java-14-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>14</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<pmd.plugin.version>3.13.0</pmd.plugin.version>
<pmd.core.version>6.23.0</pmd.core.version>
</properties>
<modules>
<module>core</module>
<module>tasks</module>
</modules>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</reporting>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<targetJdk>${java.version}</targetJdk>
<rulesets>
<!-- https://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html -->
<ruleset>/rulesets/java/maven-pmd-plugin-default.xml</ruleset>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://github.com/pmd/pmd/blob/master/pmd-core/src/main/resources/rulesets/internal/all-java.xml -->
<ruleset>/category/java/bestpractices.xml</ruleset>
<ruleset>
custom-ruleset.xml
</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<id>check pmd and fail</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.core.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.core.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
The difference is that now the pom has child modules and <packaging>pom</packaging>.
This part fails mvn clean install
<ruleset>custom-ruleset.xml</ruleset>
Error messages
[INFO]
[INFO] pmd-java-14-example ................................ SUCCESS [ 0.922 s]
[INFO] core ............................................... FAILURE [ 1.929 s]
[INFO] tasks .............................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.980 s
[INFO] Finished at: 2020-05-03T13:15:45+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd (pmd) on project bst-core: Execution pmd of goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd failed: org.apache.maven.reporting.MavenReport
Exception: Could not find resource 'pmd-java-14-example\core\custom-ruleset.xml'. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
What I tried
As I understand, maven uses custom-rulesets.xml file for parent modules - it is successful, then it tries to use this file for child module, and of course, this file is not present in the child module.
I tried to set a property - absolute path to the location of custom-rulesets.xml. It works, however, I cannot push it into github.
I can also push this custom rule set to git hub and add a link to github repository and file and use it.
This is a better alternative.
And of course, I do not want to copy-paste this file across different children modules.
I would suggest that you place your rulesets outside the Maven projects, like you are doing for the other rulesets.
The question is whether your custom ruleset should only be checked for those child modules in that project. Or is it rather something you would like to check for all Java modules?
If you want to check all your Java modules, just add the ruleset with its absolute path to your existing PMD plugin configuration. The PMD plugin ignores POM projects.
If it's a specific ruleset for just that project and just those child modules, I would configure that in the parent project (the POM project).
Please, post your answers because this is a quick work-around which I need now!
<ruleset>../custom-ruleset.xml</ruleset>.
Explanation, my project structure is one parent that has two children modules. As the children are on the same level in the directory tree, ../ thus it works.
However, it will not work, if there is src folder in parent, or if a child module has children as well.

maven build with my local-module artifactory (library)

I created a project named app-service that uses a core module(app-core). I include this core module in my project as maven dependency through <systemPath> that reside in project base directory.
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/app-core-0.1.1-SNAPSHOT.jar</systemPath>
</dependency>
I configure maven and run goal
mvn clean package install
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 44.733 s
[INFO] Finished at: 2017-04-05T12:27:46+05:30
[INFO] Final Memory: 29M/533M
[INFO] ------------------------------------------------------------------------
After BUILD SUCCESS $CLASSPATH does not contain the app-core.jar file, expect this it include all dependencies that listed in maven <dependencies>
I want to use this app-core module as compile scope. When I try this it prompt error
[ERROR] 'dependencies.dependency.systemPath' for app-group:app-core:jar must be omitted. This field may only be specified for a dependency with system scope. # line 71, column 25
Note: I do not upload this app-core on public repository due to security purpose. I want to use through project base directory because I need to deploy the same on Heroku.
Update
I googled it and found a plugin that installs local repository.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${project.basedir}/app-core-0.1.1-SNAPSHOT.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
But the result is same......
You should follow this guide to Adding Unmanaged Dependencies to a Maven Project on Heroku. To summarize the guide, run:
mvn deploy:deploy-file -Durl=file:///path/to/app-group/app-core/ -Dfile=app-core-1.0.jar -DgroupId=app-group -DartifactId=app-core -Dpackaging=jar -Dversion=1.0
Then add this repository to your pom.xml:
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/repo</url>
</repository>
Then use the dependency i your pom.xml:
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>1.0</version>
</dependency>
I think the error is telling you to delete part how you setup your dependency.
Try omitting parts of it such as:
<systemPath>${project.basedir}</systemPath>
..and working from there.
Edit:
I would also try the most limited amount of information.
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
</dependency>
Try changing the project version even. I've had problems with maven dependencies not pulling from repositories because the pom.xml was written incorrectly. The way I ended up solving them was to keep trying different ways of writing the dependency until it actually pulled it correctly.

How to read depedency version from properties file [duplicate]

This question already has answers here:
Reading Properties file from POM file in Maven
(2 answers)
Closed 6 years ago.
I have one maven project. I want to externalise maven dependancy version to external property file.
I tried with property file plugin I is not reading property file.
config.properties
springframework.version=4.2.5.RELEASE
POm.xml file
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.estuate.test</groupId>
<artifactId>testPom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>pre-clean</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>config.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
still I am gettting error message as
build.plugins.plugin[org.codehaus.mojo:properties-maven-plugin].dependencies.dependency.version' for org.springframework:spring-core:jar must be a valid version but is '${springframework.version}'
Please help me out.
When you add a plugin with a custom execution, like you do, then you must realise that it only executes in the phases you specify. You specify pre-clean phase, which is part of the clean lifecycle, and not part of the build lifecycle.
Maven is composed of lifecycles, which execute up to a given phase. A command of mvn compile actually means run the build lifecycle all phases up to and including the compile phase.
Two of the standard lifecycles are (not complete list):
clean :: pre-clean -> clean -> post-clean
build :: validate -> compile -> test -> package -> verify -> install -> deploy
The dependencies specified are probably used by the standard plugins for the compile phase, so the properties needs to be available at that time.
When you state the pre-clean phase, the properties are available when you execute mvn clean.
For the properties to be available at compile phase, you should probably bind to the validate phase instead.
Although very verbose, there is actually quite a few hints running in debug mode with mvn -X, but it may be too much to comprehend at first.
Some more information about maven lifecycles here : https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
you can use maven properties plugin which is one of the suggested way to read property files in maven projects. below is the plugin details
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>Versions.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
Version.properties file looks like
springframework.version=4.2.5.RELEASE
I think .
You should assign a Spring version to the springframework.version statement.
For example
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.1-REALESE</version>
</dependency>
then the other dependencies
<dependency>
<groupId>...framework...</groupId>
<artifactId>spring-...</artifactId>
<version>${springframework.version}</version>
</dependency>

Maven building jar of a war for another project's tests

PROJECT-A is a war. It has some code I want to use in the unit tests of PROJECT-B.
To do this, I think I need PROJECT-A to output a jar with it's classes. Here is an excerpt of the POM:
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>au.com.name.redacted</groupId>
<artifactId>PARENT-PROJECT</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PROJECT-A</artifactId>
<packaging>war</packaging>
... snip
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>PROJECT-A-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
When I run
cd PROJECT-A
mvn clean install
it outputs
[INFO] --- maven-jar-plugin:2.4:jar (PROJECT-A-jar) # PROJECT-A ---
[INFO] Building jar: D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) # PROJECT-A ---
[INFO] Installing D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar to C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war
I do see
D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.war
but I do not see the jar in in my repo - only the war.
C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war
So then in PROJECT-B I am attempting to have it get PROJECT-A as a dependency..
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>au.com.name.redacted</groupId>
<artifactId>PARENT-PROJECT</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PROJECT-B</artifactId>
<packaging>war</packaging>
... snip
<dependency>
<groupId>au.com.name.redacted</groupId>
<artifactId>PROJECT-A</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
And it fails with
[ERROR] Failed to execute goal on project PROJECT-B: Could not resolve dependencies for project au.com.name.redacted:PROJECT-B:war:1.0: Failure to find au.com.name.redacted:PROJECT-A:jar:1.0 in http://repo.server.com.au:8081/artifactory/repo was cached in the local repository, resolution will not be reattempted until the update interval of repo has elapsed or updates are forced -> [Help 1]
I've used war plugin for this purpose. Maybe this will help:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
It generates war and jar with sources after build which will be commited to local m2 repo. Configuration <attachClasses>true</attachClasses> is important.
In pom of project B use something like this (important part - <classifier>classes</classifier>):
<dependency>
<groupId>projectA-groupid</groupId>
<artifactId>projectA-artifactId</artifactId>
<version>projectA-version</version>
<classifier>classes</classifier>
</dependency>

Maven plugin not running where I want it to

I have a maven project that I want to load properties from a file when things happen. I've got the codehaus properties-maven-plugin and I need it to run automagically.
If I run mvn test or mvn compile, the task plugin load the properties file just fine.
I see this in the output:
[INFO] --- properties-maven-plugin:1.0-alpha-2:read-project-properties (default)...---
When I run mvn flyway:init, say, I do not see it run and the properties do not get loaded.
Here is the pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.foo</groupId>
<artifactId>workflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Foo Framework</name>
<description>Foo framework</description>
<properties>
<postgre.version>9.1-901.jdbc4</postgre.version>
<flyway.version>2.2.1</flyway.version>
</properties>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgre.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<url>${url}</url>
<user>${user}</user>
<password>${pass}</password>
<locations>
<location>classpath:sql/pgsql</location>
</locations>
</configuration>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgre.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/db.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Can anyone suggest what I can do, or some documentation I can read to make this work? I have read plenty of maven documentation and cannot seem to understand it well enough to make this happen. I thought what I have here would make it run during the initialize phase, but evidently not...
You are close. There is a difference between running Maven using a phase, and using a goal. Start with a quick review of the Maven lifecycle documentation.
When you run mvn compile, Maven runs all goals bound to the compile phase, plus anything bound to earlier phases. You have bound the properties:read-project-properties goal to the initialize phase. initialize is one of the first phases, so read-project-properties executes when compile, or test, or any of the later phases are used in the command.
When you run mvn flyway:init, you are only running a single goal, not the entire lifecycle. Thus, the only thing that runs with that command is the flyway-maven-plugin's init goal, nothing else.
If you want those two goals to run together, you could try binding the flyway:init goal to the initialize phase (if that is not the default; I could not tell from reading the plugin's documentation). Then, mvn initialize will run both.
If you don't want to bind flyway:init to a phase, then you may run both goals explicitly with mvn flyway:init initialize.

Categories