Slash in property name in pom.xml - maven

I have a problem, what I cannot solve.
I want to build my eclipse cdo server with Tycho/Maven and also I want to have the prossibility to start my cdo-server.product out of eclipse.
The cdo server it set up like this: http://wiki.eclipse.org/Setting_up_a_CDO_Server
No there is my problem:
If I choose:
-Dnet4j.config="${resource_loc:/cdo.server/config}"
I can start it out of eclipse, but if I want to start the built CDO-Server.app it cannot find this folder.
If I choose:
-Dnet4j.config="../../../../../../../../../../cdo.server/config"
I can start the built CDO-Server.app, but I can't start it ou of eclipse.
This is all logical, but now I decided to make ${resource_loc:/cdo.server/config} as a property in my pom.xml file.
But if I write it like this:
<properties>
<resource_loc:/cdo.server/config>../../../../../../../../../../cdo.server/config</resource_loc:/cdo.server/config>
</properties>
I get the exception, that this is not a parseable POM because of the slash in the tag.
If I want to make it like this:
<properties>
<resource_loc:>
<cdo.server>
<config>../../../../../../../../../../cdo.server/config</config>
</cdo.server>
</resource_loc:>
</properties>
It also is not a parseable POM. Is there any possibility to use ${resource_loc:/cdo.server/config} as a property?

The problem in your property definition are the slashes in the name of the property.
The following will fail parsing the pom.xml:
<resource_loc:/cdo.server/config>yx</resource_loc:/cdo.server/config>
or
WhatEverValue
It will also not working if you try to use / as a replacement for the slash in the entity name.

you may want to try http://mojo.codehaus.org/properties-maven-plugin/read-project-properties-mojo.html to read the properties from a separate file instead in order to get around the XML well-formedness limitation in pom.xml.

Related

vscode-java: picj pom-k3d.xml instead of pom.xml

Currently, I've two "pom.xml" files into my project:
pom-k3d.xml
pom.xml
pom-k3d.xml contains some additional plugins and dependencies.
vscode is getting by default pom.xml.
Is there any way to set vscode pick pom-k3d.xml file instead?
Is there any way to split a "pom.xml file into two modular ones?
Any ideas?

How to provide my "rules.xml" file in -DrulesUri argument of maven versions:update-properties without needing to specify it in pom.xml file?

I am using Maven Versions plugin's update-properties goal to update properties in pom.xml of multiples projects. (https://www.mojohaus.org/versions-maven-plugin/update-properties-mojo.html). I want the latest version of the dependency for properties.
Now, there are some binaries with wrong versions. I want my code to ignore these versions. For this, I have created my "rules.xml" file. I want to provide it as -DrulesUri argument to update-properties goal.
I already tried specifying this rules.xml file in pom.xml file of project as shown on (Maven versions plugin: reference a rule.xml from a maven dependency?). This worked as the plugin could successfully ignore specified versions in rules.xml. So, there is no problem with rules.xml file. But, this is not useful in my case, since there are many projects involved and I cannot update pom.xml of each project.
The documentation of rulesUri property says "URI of a ruleSet file containing the rules that control how to compare version numbers. The URI could be either a Wagon URI or a classpath URI (e.g. classpath:///package/sub/package/rules.xml)." This much documentation is not helping me. I would want an example on how to specify the rules.xml file in -DrulesUri argument. I tried specifying the rules.xml file as local relative path / absolute path. But, update-properties goal does not seem to recognize the rules and just proceeds similar to execution without -DrulesUri argument. I tried reading https://maven.apache.org/wagon/ to understand Wagon URIs. But, I did not find a simple way to upload my rules.xml somewhere and then use this Wagon URI to specify in -DrulesUri.
This is my rules.xml file :-
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" comparisonMethod="maven" xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 http://mojo.codehaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd">
<ignoreVersions>
<ignoreVersion type="regex">25243.*.*</ignoreVersion>
</ignoreVersions>
<rules>
</rules>
</ruleset>
Use -Dmaven.version.rules
Example:
mvn versions:display-dependency-updates -Dmaven.version.rules=file:///$HOME/.m2/rules.xml
From AbstractVersionsReport.java source code:
/**
* URI of a ruleSet file containing the rules that control how to compare
* version numbers. The URI could be either a Wagon URI or a classpath URI
* (e.g. <code>classpath:///package/sub/package/rules.xml</code>).
*
* #since 1.0-alpha-3
*/
#Parameter( property = "maven.version.rules" )
private String rulesUri;

Maven: passing requiredProperty values in archetype-metadata.xml through properties or yaml file

I want to generate maven archetypes by using custom properties through archetype-metadata.xml's requiredProperty -
<requiredProperties>
<requiredProperty key="proxy-name">
<defaultValue>${proxy.name}</defaultValue>
</requiredProperty>
<requiredProperty key="proxy-desc">
<defaultValue>${proxy.description}</defaultValue>
</requiredProperty>
</requiredProperties>
However, my requirement is to initialize these requiredProperty values using key:value pairs provided either in a properties file or a yaml file, so that I can get these values injected in pom.xml of custom project structure under archetype-resources.
<properties>
<proxy-name>${proxy.name}</proxy-name>
<proxy-desc>>${proxy.description}</proxy-desc>
</properties>
I do not want to provide the values to these properties via command line or by providing default values. I want the initialization of these property values dynamic based on reading an external properties file when I run the mvn archetype:generate command.
Is this even possible? My apologies in advance if the question seems too vague or really elementary. This is my first experience dealing with custom maven archetypes.
P.S - I have tried using the yaml-properties-maven-plugin, however the values still don't get populated in archetype-resources pom.xml, which normally takes values when initializing property values via command line.
So, to answer my own question and to help anyone who is into the same problem:
What I did, is to also include an archetype.xml in META-INF/maven which takes in the resource properties file name to use to substitute custom values in the archetype-metadata.xml. Here's how the archetype.xml looks:
<?xml version="1.0" encoding="UTF-8"?>
<archetype>
<id>quickstart-archetype</id>
<sources/>
<resources>
<resource>archetype.properties</resource>
</resources>
</archetype>
This way the yaml-maven-properties plugin reads the yaml file and writes an archetype.properties file in the src/main/resources folder which also contains the archetype-resources folder structure for archetype generation.
Let me know in case anyone needs more clarification on how I achieved this.

Is there a way to post-process project generated from archetype?

Say I have an archetype and I generate a project from it. But I would like to resolve placeholders in a property file of the project I generated on after generation time by passing the value for placeholder through command line.
For example having the following command line:
mvn archetype:create -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=1.0 -DgroupId=... -DartifactId=my-project -Dversion=1.0-SNAPSHOT -Dhello=Hello!
say the archetype contains app.properties (as part of project which is being generated) with the following content:
greeting=${hello}
Is it possible to replace ${hello} with "Hello!" right after project has been generated as a result of mvn archetype:create command?
Yes this is possible. From the advanced usage guide for maven archetypes:
If the user wants to customize the generated project even further, a groovy script named archetype-post-generate.groovy can be added in src/main/resources/META-INF/. This script will end up in the generated archetype's META-INF folder and will be executed upon creating a project from this archetype. This groovy script has access to the ArchetypeGenerationRequest object, as well as all the System.getProperties() and all the archetype generation properties the user has specified.
You could define additional properties in the archetype, following the format:
https://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html
For example:
define the file: src\main\resources\META-INF\maven\archetype-metadata.xml
<archetype-descriptor
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
name="modelant.metamodel.api">
<requiredProperties>
<requiredProperty key="package"><defaultValue>${groupId}.${artifactId}</defaultValue></requiredProperty>
<requiredProperty key="parentGroupId"><defaultValue>${groupId}</defaultValue></requiredProperty>
<requiredProperty key="parentArtifactId"><defaultValue>${artifactId}</defaultValue></requiredProperty>
<requiredProperty key="parentVersion"><defaultValue>${version}</defaultValue></requiredProperty>
<requiredProperty key="metamodelUrl"/>
</requiredProperties>
</archetype-descriptor>
Here you see that it defines additional required properties, so they have to be mandatorily provided within the dialog, where:
some properties may have no value - see metamodelUrl
some properties may have default values either
-- as static text
-- or referring the values of the previously defined standard properties: groupId, artifactId, version
some poperties may override the values of the standard properties - the "package" property. Here it is redefined.
Please note:
the https://maven.apache.org/archetype/maven-archetype-plugin/advanced-usage.html Apache maven page on archetypes refers just calling "mvn install" in order to publish the artifact in the local repository. This is not enough - use: mvn clean install "archetype:update-local-catalog"
the https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html Apache maven page states that the proeprties are referred using "property name" expressions. This is not correct - the properties are allowed to be used in the filtered resources, treating them as velocity templates, thus the references are ${property name} and #if, #for, etc. statements could be used there
Not sure I understood correctly. For post processing after project creation you could use the param -Dgoals and invoke your custom plugin.
Am not sure about your requirement, but why cant you do the same during the project generation itself ?

export all defined maven project properties to file?

I have a maven 3 project. In the POM, I define numerous <properties> - some under <project>, others under specific <profile>. is the a way in maven to export all declared properties to a .properties file?
My current way of doing so is to:
create env.properties file in src/main/resources
for each property 'myProp' add this line to env.properties: myProp=${myProp}
enable resource filtering during builds
Seems like there ought to be a way to eliminate step 2 above...
thanks,
-nikita
Use properties-maven-plugin and its write-project-properties goal.
If I understand your requirements correctly, you can do this using the antrun-plugin coupled with Ant's echoproperties task. An example of this configuration is in the StOf question here.

Resources