Inject variables to external files from pom.xml using maven - maven

I want to populate some external files from the same project with some variables from pom.xml before the compilation phase.
<properties>
<version.Var1>1.0.0-1</version.Var1>
<version.Var2>1.0.0-2</version.Var2>
<version.Var3>1.0.0-3</version.Var3>
</properties>
What is the best approach for doing that? Let's imagine that I have a file called versions.xml, and I want to use the previous versions(Var1,Var2,Var3) there.
I want to know which is the best approach.

You can try to import properties into project using:
properties-maven-plugin

Related

Setting application-specific properties on deploy by maven

I would like to deploy java app twice to one tomcat server, each time with different environment properties.
I would like to find a way like
mvn tomcat7:deploy -Denvironment=local
I don't mind using other maven plugin.
With no need to change files after deploy.
Is it somehow possible?
Thank you for you answer.
You can have a configuration file (something like e.g. application.properties) that you enable resource filtering for. You could then parameterize the values in that configuration file, and pass in different parameter values for each of the deployments (-Dkey1=value1 -Dkey2=value2).
You could pass the different parameter values on the command-line, or you could cement them in different profiles, and just activate the appropriate profile from the command-line (-Psecond-deployment).
What seems a little unfortunate from the suggested approach in your question is: in its most basic sense Maven is a build tool, meant to produce a consistent artifact from a build. This is no longer the case with the suggested approach in your question. If this is a web application that is never used as a dependency of other module, this may be fine. But just highlighting that different deployment configurations is a deployment concern, not a build concern.
I used little workaround.
I created different profiles which I use to update application property.
example:
In my POM I have:
<profiles>
<profile>
<id>local</id>
<properties>
<environment>local</environment>
....
</properties>
</profile>
and in application.property file I have
environment=#environment#
(That # are correct! )
This way I can deploy my app with defined environment by using command
mvn -P local tomcat7:deploy
and use environment variable anywhere else as ${environment}

is there a placeholder for src/main/webapp/WEB-INF directory in Maven

Is there a predefined placeholder for src/main/webapp/WEB-INF directory in maven which can be referred by ${XYZ} ?
If not, is there a way to define such a placeholder?
There isn't really a need for predefined variables that are very much tied to a particular type of project module and packaging standard, with $basedir you have access to any module folder you want. So also ${basedir}/src/main/webapp/WEB-INF.
And Maven allows you to define your own properties.
<project>
<properties>
<webinf.basedir>${basedir}/src/main/webapp/WEB-INF</webinf.basedir>
</properties>
...
</project>
Then you simply have access to the path as ${webinf.basedir}. Use whatever property name you like of course.
Refer to the Maven documentation for a nice overview of available path properties.

Use maven to "extend" existing jar file

My project contains a couple of class which have to be integrated into an final jar file.
So my current "workflow" looks like:
mvn compile && jar uf contribution.jar -C target/classes .
I guess this could be done with the maven-jar plugin or through maven-assemblies but can't figure out how.
Simple - add the element <packaging>jar</packaging> to your pom.xml and invoke the following:
mvn package
See Maven build lifecycle for more information. It really is a must-read if you're planning to use Maven for you project.
Edit
Thanks for the update, I know understand what you mean. There may be a better way to do this, but this is what I use:
Maven will automatically include this jar if you add it as a dependency in the usual way, i.e:
<dependencies>
...
<dependency>
<groupId>some.group</groupId>
<artifactId>contribution</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
The question is how to ensure that maven can find the jar? If the jar were in the Maven Global repository it would find it automatically, so always check this first. If not, then you will have to create your own repository to store custom jars. This may not be the most clever way to do it, but it's how I do it. Note that this is not the same thing as the cache stored in the .m2 folder.
Let's say we want to use c:\maven\repo as our local. To do this, first ensure the folder exists then add the following lines to your pom.xml:
<repositories>
<repository>
<id>my-repo</id>
<url>file://c:\maven\repo</url>
</repository>
</repositories>
Next, you will need to manually add the contribution jar to this repo. To do this execute the mvn deploy:deploy-file command and give it the appropriate values. See here for more information.
Now when build/compile/package your original project, it should include contribution.jar in the packaging as it is a dependency.
Hope this helps :)

Maven JAXB plugin and locale for generated output

I'm using the maven-jaxb2-plugin (from org.jvnet.jaxb2.maven2) to generate classes from an XSD schema. All is working fine and the generated classes are all that I need. Except for one thing, all the generated comments inside those classes are using my computer locale (which is french).
I'd like to be able to change the locale of the generated comments (to use english for example). Can it be done through the plugin configuration or the project configuration in the pom.xml ? Or must it be done in my shell when I run the mvn command?
I tried setting the project.build.sourceEncoding but that doesn't change the locale. I tried setting the user.language or user.country but that doesn't have any effect on the output.
There is an option to pass some arguments to the xjc command that I could use, but I cannot find anything related to locale when calling xjc.
The best thing is to put the following into your pom:
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
The above will also influence the maven-compiler-plugin etc. Furthermore the jaxb2-maven-plugin will also be influenced by the above property (${project.build.sourceEncoding}).

Maven: Selecting Parent Project Based On Profile

I have a maven project - it is a plugin for jenkins. It's parent should be a:
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
But at the same time this plugin can be also used for hudson, without changing any line of code. But the parent project for it should be:
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hudson-plugin-parent</artifactId>
<version>2.0.1</version>
</parent>
Can I specify 2 different profiles for that and use them to build plugin for jenkins or hudson accordingly? So that I call something like that:
mvn package -P jenkins
or
mvn package -P hudson
I have tried to specify properties in profiles, but those are not replaced by their values inside the <parent> tag. So is there any other possibility to build plugin for both, but with as much as possible common code and files?
Added: So, if I cannot do that, what should I do then? How to refactor? What the new structure should be?
As already mentioned, this is not possible.
Also, it is not possible to set a property for the parent's version as the interpolation for that happens a lot earlier than the handling of the profiles.
I would suggest that you create a masterbuild project as follows:
master
|-plugin-jenkins
|-plugin-hudson
|-plugin-assembly
The master should build all three as usual. However, in the assembly, you could add each of the two plugins as dependencies in separate profiles. And... each of these plugins can have the parent you like.
This is obviously somewhat a deviation from the Maven convention, but I believe it is a solution to your problem.
It's not possible because the tag "parent" is not available in the profiles section of the pom.
Currently we decided to stick with 1 repository and 2 separate pom.xml files, giving maven key which pom.xml use to build the project.
mvn package -f pom-jenkins.xml
mvn package -f pom-hudson.xml
No you cannot do that. you will have to refactor somehow to avoid the necessity.
As mentioned already not possible. I would suggest to make separate projects for jenkins plugin and hudson plugin. I assume that in not that far future that will not work anymore cause Hudons and Jenkins will diverge.
In general, you should be able to set the {group,artifact}Id and version of the parent POM via Java System Properties or Environment Variables, but it seems there is a Bug in Maven which will only be fixed in 4.x:
https://issues.apache.org/jira/browse/MNG-624
Another solution is to delegate the inclusion of the parent POM to your own parent POMs which you reference in the relativePath element, and change the content of the target e.g. via a symlink or cp command.
So in the main POM you would write:
<parent>
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<relativePath>./my-parent.pom</relativePath>
</parent>
And in my-parent-jenkins you would just put:
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
The same project information with the block for hudson you put in my-parent-hudson.pom.
No you can either use
ln -s my-parent-jenkins.pom my-parent.pom
or
ln -s my-parent-hudson.pom my-parent.pom
to include the respective parent POM without the need to maintain two different main POM files for your project.
In case POM does not exist at the place referenced in relativePath, Maven will look up the POM in the remote repository[1], which is also an easy way to overwrite a parent POM locally.
[1] http://maven.apache.org/components/ref/3.3.9/maven-model/maven.html#class_parent

Resources