Passing Maven Compiler Options From The Command-line - java

I am setting up several projects on a continuous integration server, some of which I don't have access to change the source code to, The server is a linux box, I am running into a problem where maven encoding needs to be changed to UTF8 to be able to compile on the box. Since I don't have access to modify the pom file, I was wondering if I can pass the compiler options as a command-line param? The project uses maven compiler 2.0 and I tried passing -Denconding=UTF8 without success.

You can use the Maven property "project.build.sourceEncoding".
So something along the lines of
mvn clean install -Dproject.build.sourceEncoding=UTF-8 should accomplish what you need.
This is equivalent of
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
in your pom.xml.
Edit: As a point of reference, there is the following link available POM Element for Source File Encoding showing the nuances between these properties for both Maven 2.0 and 3.0

Related

Maven project version interpolation from a property file

We are using a Maven for a while in our project and want to automate the release process little bit. we came up with the following idea so that the version will be maintained by developers or in SCM instead of in DevOps tool like jenkins/bamboo.
Anyone following below process instead of setting the interpolation value in arguments as "mvn install -Dapp.version=1.0.0-SNAPSHOPT"
The process we like to follow is to supply the Maven project version through an external property file.
let's assume the following partial POM.xml excerpt as example.
<project>
<groupId>com.home.diary</groupId>
<artifactId>journal</artifactId>
<version>${app.version}</version>
<packaging>war</packaging>
</project>
let's assume i have an version.properties file in my SCM with following content
app.version=2.0.0-RELEASE
while running the mvn goal
mvn install
i want the artifact generated as
journal-2.0.0-RELEASE
I tried using plugin properties-maven-plugin from org.codehaus.mojo
as discussed here How to read an external properties file in Maven
but it's not working.
Anyone did this? could you please share your implementation/ideas?
This is not possible.
First of all: Why not just manage the version in the <version> tag itself? It is the easiest thing and fulfils your requirement (the developer manages the version in the SCM).
If you don't want this, you need to supply the version either in the POM itself or through the command line. Reading external properties with something like the properties maven plugin will always happen too late, i.e. after the version tag is already read.

keycloak-domain-extension clean build does not work as expected when deployed

We are having some issues with deploying the examples for keycloak extensions, more specifically this one:
keycloak-domain-extension(GitHub)
We have imported the entire keycloak repo into IntelliJ using maven import.
Added a compiler version to the pom-file, since it defaulted to 1.5 some how…:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
now we are able to build a JAR artifact from IntelliJ
but after deploying the jar to JBoss using the specified command from the README.md of the repo. we get:
RESTEASY003815: Subresource for target class has no jax-rs annotations.: org.keycloak.examples.domainextension.rest.ExampleRestResource
Does this look familiar to someone? any ideas very appreciated…
Seems like IntelliJ's buildchain did something here,I just imported the pom-files, so IntelliJ probably needed some additional setup...
When building with mvn install in an external terminal and deploying according to the readme, everything seems to work.
When importing in IntelliJ i selected all the targets, if you only import the preselected/default targets build works as expected.

maven release plugin - manipulating project release version

I'm trying to configure in Jenkins a maven release build with customized release version that includes the branch from which the release was made.
It looks something like this:
release:prepare -DreleaseVersion=${project.version}-${GIT_LOCAL_BRANCH}-Release release:perform
everything works fine, except that the 'project.version' placeholder, which calculated based on the pom, contains the '-SNAPSHOT' postfix.
is there other placeholder which I can use to get it without the '-SNAPSHOT'?
I know that the maven release plugin, by default, will set the right version - only I want to manipulate that value.
See the offical docu https://maven.apache.org/maven-ci-friendly.html.
There are 3 properties maven supports since 3.5: ${revision}, ${sha1} and ${changelist}. So you can use something like
<version>${revision}${changelist}</version>
...
<properties>
<revision>1.3.1</revision>
<changelist>-SNAPSHOT</changelist>
</properties>
And call it with
release:prepare -DreleaseVersion=${revision}-${GIT_LOCAL_BRANCH}-Release
But the maven-release-plugin does not work nicely together with the CI friendly versions. Read: it will probably overwrite your placeholders in the version tag. So in the end you might just manipulate the -SNAPSHOT string via bash commands.
is there other placeholder which I can use to get it without the '-SNAPSHOT'?
Unfortunately I believe the answer to your question is "no, there is no built-in variable that will satisfy your needs". However, you could try creating a property and then using that property inside of your project's version tag.
I am not able to test this as I don't have the appropriate environment set up and it's rather late so I'm not going to have the time to set it up right now. I will outline my idea and perhaps it will help you:
You could do something like this in your POM:
<version>${artifactReleaseVersion}-SNAPSHOT</version>
<properties>
<artifactReleaseVersion>0.0.1</artifactReleaseVersion>
</properties>
Then run your goals using that property:
release:prepare -DreleaseVersion=${artifactReleaseVersion}-${GIT_LOCAL_BRANCH}-Release release:perform

Replace Maven properties in pom.xml via command line

I want to replace a Maven property in a pom.xml file with a command line call via some Maven plugin.
<properties>
<my.property>ISO-8859-1</my.property>
<properties>
Do you know of a plugin which is able to do that?
The versions plugin takes very long since it checks whether some dependency is available. Besides that it doesn't work in my case.
All you need to do is add the following when running maven on the command line
-Dmy.property=propertyValue

How can I run unit tests with different versions of a library w/o changing it manually?

Unit test is in Java, using Maven to build & run. I can use either local jars "outside" Maven or local Maven repository.
However, I'd like to figure out a way to do this automatically somehow, without changing the pom.xml->running ->changing the pom.xml
Is there any other way except the above or creating pom.xmls which only differ in the specific library version?
(I'm using IntelliJ if that's of any use)
You could also read the version of your lib from a property, like
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsp.api.version}</version>
</dependency>
You can set this property in several ways, e.g. loading build-specific properties file.
You could even specify it as a parameter when running the build. On the command line svn, this is something like
mvn -Djsp.api.version=1.8 install
Don't know how to specify such an property when running Maven from inside IntelliJ, but I'm sure it's possible....
This approach would give you full flexibility to freely speficy the lib version for each build. But if you only have a limited number of versions you want to choose from, using profiles is probably the better way to go. Just define a profile for each version number and the always tell Maven which profile to use when you run a build.
What you are looking for is profiles in Maven with properties.
You use a property to specify the version number and you can use profiles to specify what the property actually is and then specify which profile to use when you run Maven.

Categories