Maven not passing overriden properties to ant - maven

Currently running into a weird problem with a Maven build that is making a call to ant, within the parent pom the following property is defined: -
<jboss.home>${env.JBOSS_HOME}</jboss.home>
I am trying to override this by passing in -Djboss.home when I run Maven.
This build contains a profile that when activate calls an Ant build: -
<ant antfile="build.xml" inheritRefs="true">
<target name="all"/>
</ant>
The Ant script uses the property: -
<property name="jboss.dir" value="${jboss.home}"/>
An subsequently outputs it: -
<echo message="jboss dir is: ${jboss.dir}"/>
The problem is that the value it output is ${env.JBOSS_HOME}
Other properties in the parent pom I can override from the command line.
Even the jboss.home property does appear to be overridden if use elsewhere within the Maven build.
After trying various combinations of command it almost appears that the set of properties passed to Ant are resolved from the poms BEFORE any overrides from the command line. If I set the JBOSS_HOME environment variable then all places this variable is used have the correct values.
Is there something I am missing to be able to override this variable on the command line and have the overridden value used in the Ant script?

Stumbled into this problem today, and did discover an answer. This link provides some background: http://technotes.khitrenovich.com/properties-resolution-maven-implications-antrun-plugin/
Maven properties will work just fine if the property is referenced inline within the <target>. e.g:
<execution>
..
<configuration>
<target>
<echo message="Command-line override of external.property will work OK like this: ${external.property}"/>
</target>
</configuration>
</execution>
However, if you use an external ant file written like this, many/most properties will work, BUT command-line overrides will not be passed into the ant file. You will get the non-overridden value instead.
<execution>
..
<configuration>
<target>
<property name="external.property" value="${external.property}" />
<ant antfile="build.xml" target="all" />
</target>
</configuration>
</execution>
Instead, for external ant files, use this syntax. The command line value will be passed through correctly to the external antfile:
<execution>
..
<configuration>
<target>
<ant antfile="build.xml" target="all" >
<property name="external.property" value="${external.property}" />
<ant/>
</target>
</configuration>
</execution>

The problem is that ${env.JBOSS_HOME} is a system environment variable and you are instead passing to the JVM a system property -Djboss.home=.... These are two different things. Apart from that, any variables, args and so on in the Java world are case sensitive.

You seem do be doing all the right things, it works for me, I have made a working example with 2 pom.xml files. The parent pom looks like this:
<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.stackoverflow.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<name>Test</name>
<packaging>pom</packaging>
<modules>
<module>test-ant-properties</module>
</modules>
<properties>
<jboss.home>${env.JBOSS_HOME}</jboss.home>
</properties>
</project>
Then I created a module pom.xml in the sub-folder test-ant-properties that looks like this:
<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.stackoverflow.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</parent>
<artifactId>test-ant-properties</artifactId>
<name>Test maven ant properties</name>
<profiles>
<profile>
<id>jboss</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>jboss-ant</id>
<phase>install</phase>
<configuration>
<target>
<property name="jboss.dir" value="${jboss.home}"/>
<echo message="jboss dir is: ${jboss.dir}"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
I don't have JBOSS installed so for testing purpose I set the environement variable to test1234 like this:
set JBOSS_HOME=test1234
When I execute the parent pom with the jboss profile
mvn install -Pjboss
I get the following result: [echo] jboss dir is: test1234
When I execute the same command with the jboss.home setting
mvn install -Pjboss -Djboss.home=my_custom_variable
I get the following result: [echo] jboss dir is: my_custom_variable

Related

Properties defined in the properties section of the POM are not seen in external ant build file

I'm using maven-antrun-plugin in my pom.xml with external ant file.
It's said in plugin's document:
All of the properties available to Maven are also available in the
target configuration. However, you may want to call an external Ant
build script using the ant task. To avoid name conflicts, only a
subset of the properties are passed to the external Ant build. These
include all properties defined in the properties section of the POM.
It also includes prefixed versions of some of the commonly used Maven
properties.
So here's my pom, where I define "test.prop" property:
<?xml version="1.0" encoding="UTF-8"?>
<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>testant</groupId>
<artifactId>testant</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<test.prop>TestPropValue</test.prop>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>optional</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-index-properties</id>
<phase>install</phase>
<configuration>
<tasks>
<!--<property name="test.prop" value="${test.prop}"/>-->
<ant antfile="build.xml">
<target name="echo-prop"/>
</ant>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And just trying to echo this property in build.xml:
<project default="test">
<target name="echo-prop">
<echo>${test.prop}</echo>
</target>
</project>
This is what I get:
echo-prop:
[echo] ${test.prop}
So property is not resolved as it should, according to the doc.
And it works fine only in case if I uncomment line with explicit property declaration under "tasks" tag.
Could you please help me in understanding, what am I doing wrong?
Thank you!
You should specify a version for the antrun-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version><!--$NO-MVN-MAN-VER$-->
(...)
</plugin>
ant by default passes all properties of the parent to an underlying "ant"-call. Unless you define inheritAll=false, all properties are passed.
In this case, you should have a look at the effective pom. There a very ancient version of the antrun-plugin is defined.
As soon as you switch to a recent one, the example code works.

How does maven decide when to use the target folder for classpath

I have a question regarding how maven calculates the classpath during building. Specifically, what controls when the "target/classes" is used and when the "jar" from a repository (local/remote) is used.
I have a project on version 1.0.0-SNAPSHOT where the artifacts have NOT been installed/deployed so there is no "jar" in some repository (remote or local) to resolve them. I want to run "generate-sources" WITHOUT installing locally (no 'mvn install' run).
The structure looks like this:
parent-prj
parent-prj/sub-prj
parent-prj/gen-src-prj <--- This depends on 'sub-prj'
When I run "mvn -am -pl parent-prj/gen-src-prj generate-sources" in order to just generate some java files, it does not work:
[ERROR] Failed to execute goal on project gen-src-prj: Could
not resolve dependencies for project
mygrp:gen-src-prj:jar:1.0.0-SNAPSHOT:
Could not find artifact
mygrp:sub-prj:jar:1.0.0-SNAPSHOT -> [Help 1]
Using debug output and adding "dependency:build-classpath" I can confirm that maven ignores the presence of "sub-prj" in the reactor and looks for a "jar" somewhere which it can't find. Yet the project is printed in the reactor summary:
[INFO] Reactor Summary:
[INFO]
[INFO] parent-prj ..................................... SUCCESS [ 0.625 s]
[INFO] sub-prj ........................................ SUCCESS [ 0.018 s]
[INFO] gen-src-prj .................................... FAILURE [ 0.040 s]
The interesting thing I noticed is that running the compile goal works fine! This uses sub-prj/target/classes (as shown by dependency:build-classpath) and has no trouble generating the sources and even compiling them: "mvn -am -pl parent-prj/gen-src-prj compile"
So here are the points I want to understand:
Why does the compile goal work but the generate-sources doesn't work?
At what point does maven decide to use the output folder of previous projects on the reactor classpath instead of looking for a jar?
Is there a way for generate-sources to run directly as I want it EVEN WITHOUT having its dependencies resolved?
Regarding (3) my generation tool is a utility invoked by:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
The tool reads some XML in src/main/resources and generates Java files and does NOT need anything in its class-path (so there is no need for maven to resolve it).
Also note that I would be interested to understand (1) and (2) even if a solution for (3) is provided.
EDIT: Per comment request, adding full example
parent-prj/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sub-prj</module>
<module>gen-src-prj</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.9</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
parent-prj/sub-prj/pom.xml
<?xml version="1.0"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>sub-prj</artifactId>
</project>
parent-prj/gen-src-prj/pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>gen-src-prj</artifactId>
<dependencies>
<dependency>
<groupId>mygrp</groupId>
<artifactId>sub-prj</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>uk.co.real_logic.sbe.SbeTool</mainClass>
<systemProperties>
<systemProperty>
<key>sbe.output.dir</key>
<value>${project.build.directory}/generated-sources/java</value>
</systemProperty>
<systemProperty>
<key>sbe.validation.warnings.fatal</key>
<value>true</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>${project.build.resources[0].directory}/Examples.xml</argument>
</arguments>
<workingDirectory>${project.build.directory}/generated-sources/java</workingDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>uk.co.real-logic</groupId>
<artifactId>sbe-tool</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
EDIT: Armed with the knowledge from the answers I have come up with this workaround that allows one to achieve the desired behaviour. I list the dependencies in a profile that is active by default, then use another profile to run generate-sources with no dependencies active, like follows:
parent-prj/gen-src-prj/pom.xml
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>mygrp</groupId>
<artifactId>sub-prj</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>excludeDependency</id>
<dependencies>
</dependencies>
</profile>
</profiles>
To generate sources with above, use: mvn -PexcludeDependency generate-sources
Maven can reference only output generated in current Session (during currently executing shell command). It uses the most "mature" place to look for the "output":
If compile is run - the classes end up in the target/classes dir, thus other modules can reference that
If package is run - then target/*.jar is created and this jar file ends up in the classpath instead
If install is run - then jar file ends up in the local repository - which is what ends up on the classpath
So there are 3 factors that impede your task:
maven-exec-plugin requires dependency resolution (as pointed out by #mondaka)
Your module1 references module2
generate-sources is run before the compilation. Thus module2 is not yet prepared to be used as a dependency.
So if you want to do it your way - you'll have to run at least compile phase each time you use anything from the Default Lifecycle. Or you could write your own plugin that doesn't require dependency resolution.
This problem is related to an open maven bug:
https://issues.apache.org/jira/browse/MNG-3283
The issue says: "The problem only occurs when a plugin binds itself to the
generate-sources phase and has #requiresDependencyResolution".
I have checked that exec-maven-plugin Mojo have indeed requiresDependencyResolution = ResolutionScope.TEST. You can see that on https://github.com/mojohaus/exec-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Then, your only option is to use compile or process-classes phases. This is a Major open bug from 2007...

HOw to run Jmeter tests with Maven?

Can someone suggest how to run Jmeter tests with Maven please? This is just a simple jmeter script with http call to google.com with no variables. Just want to start with a simple one.
I've created the path C:\Jmeterautomation\src\test\jmeter\google.jmx that maven recognizes
I've created pom.xml in this directory C:\Jmeterautomation
This is what pom looks like
<?xml version="1.0" encoding="UTF-8"?>
<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.adp.ds.services</groupId>
<artifactId>01-run-with-Maven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-Maven-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
when I open command window, and go to this C:\Jmeterautomation and type mvn verify, I'm getting error mvn is not recognized as internal or external command
I've download this jmeter-maven-plugin-1.4.1.jar and kept in C:\Jmeter\apache-jmeter-2.13\lib\ext.
what am I doing wrong here?Tried all possible ways that I know off,Is there any step by step instructions that can go through the process, please suggest.
Thanks in advance
I'm getting error mvn is not recognized as internal or external command
Please install maven first:
https://maven.apache.org/guides/getting-started/windows-prerequisites.html

Maven multi-module web application (Hot swap)

I have a multi module web application.
The structure is:
Parent
|
| - - Child 1
| - - Child 2
| - - Web
And the web project depends on the two child modules
When using my IDE to build my project I was used to the IDE building the classes in the WEB-INF/classes folder. This was nice as the web server noticed the new classes and either restarted or hot deployed these files. With maven it seems that I have to package the whole thing from scratch every time.
I would like to find a way in maven such that i can avoid running mvn:clean mvn:install mvn:war:inplace. Instead I would like a mvn:comile, and then the stuff just there.
I hope you understand what I mean. Testing the web app is extremely slow when you always have to build all the project jars and run som war command before things are updated.
The web apps 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>
<parent>
...
</parent>
<artifactId>web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>bla.bla.bla</groupId>
<artifactId>bla_child1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>bla.bla.bla</groupId>
<artifactId>bla_child2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.4.v20130625</version>
<configuration>
<scanIntervalSeconds>30</scanIntervalSeconds>
<webApp>
<contextPath>/blabla</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
A possible solution to your problem could be to build a skinny war with the maven-war-plugin (or at least exclude child1 and child2 if you have other dependencies as well) and add the target/classes folders of the child-projects to your classpath. This could be done with the Build Helper Maven Plugin's add-resource goal.
Disclaimer: I didn't try this myself but in theory it could work :)
This is my own temporary answer to this problem.
It would be nice if it could be done with maven, but I haven't been able to find a solution. Therefore I have bound an ant target to the compile phase of the web module.
This is probably not a solution that can be used anywhere, but it works in my case.
web/pom.xml:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>copy-modules</id>
<phase>compile</phase>
<configuration>
<target>
<delete dir="${project.basedir}\src\main\webapp\WEB-INF\classes"/>
<copy todir="${project.basedir}\src\main\webapp\WEB-INF\classes">
<fileset dir="${main.basedir}\modules\child-1\target\classes">
<include name="**/*.class"/>
<include name="**/*.xml"/>
</fileset>
<fileset dir="${main.basedir}\modules\child-2\target\classes">
<include name="**/*.class"/>
<include name="**/*.xml"/>
</fileset>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Now the jetty sees everytime I do a compile, which is much faster than install, war and the stuff that was needed before.

Define different value per profile for same Maven property

I'm facing a problem with maven property per profiles. I have two profiles, each one has the same property 'prop.key' with different values. When I call mvn clean package -PA -PB or mvn clean package -PB -PA both are using the same value 'B-1.0-SNAPSHOT'. I'm using maven 3.0.4.
Below my POM :
<?xml version="1.0" encoding="UTF-8"?>
<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.test.module</groupId>
<artifactId>test-module</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<prop.key>UNKNOWN</prop.key>
</properties>
<profiles>
<profile>
<id>A</id>
<properties>
<prop.key>A-${project.version}</prop.key>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>A</id>
<phase>package</phase>
<configuration>
<tasks name="a" description="a-desc">
<echo message="RUN A = ${prop.key}" level="info"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>B</id>
<properties>
<prop.key>B-${project.version}</prop.key>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>B</id>
<phase>package</phase>
<configuration>
<tasks name="b" description="b-desc">
<echo message="RUN B = ${prop.key}" level="info"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
I have found 'similar topic' but with opposite result!
Is it a bug or a feature of maven?
You can write
mvn package -PA,B
for short.
The result is the same:
[echo] RUN A = B-1.0-SNAPSHOT and
[echo] RUN B = B-1.0-SNAPSHOT
This is the correct behaviour of maven.
One property can only have one specific value per run. You can overwrite a "default value" with a version in a profile. But if you redefine it in two profiles and activate both, one of the profiles "wins".
It is not possible to have one value per profile for one and the same property. Profiles do not have their own variable scope. Properties are always global.
I would think you get this behavior because last parameter for profile gets picked.
Try
mvn clean package -PA,B
See Maven introduction to profiles.
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of
profile-ids to use. When this option is specified, no profiles other
than those specified in the option argument will be activated.
When you run mvn package -PA,B, maven will read both profiles A and B, and the properties from later one in the pom.xml file will take precedence (B is defined below A in pom.xml), result in <prop.key>B-${project.version}</prop.key> will be used.

Resources