Does Maven Have a mechanism to read the machine name - maven

I'm writing a Maven project and I'd like to include a file in the generated WAR that will contain some build time information. Typically this will be things like
The build time/date stamp
The user name of the person who built the WAR
The version of the app as specified in the POM
These are all fairly easy as there are maven properties which will give me the information I need.
I'd also like to include the machine name. I know Windows stores this information in an environment variable called "COMPUTERNAME", while *nix uses the hostname command.
Is there some platform independent way of grabbing this information so that I can write it into my text file?

I did this by invoking the Maven Ant task. Within that I used the following Ant tasks:
<tstamp> to generate a timestamp property
<propertyfile> to create a properties file containing properties like the above timestamp, the username etc.
You could use the Ant <exec> task to execute hostname and nominate an output property to write this value into.
This created a properties file in the src/main/resources dir that I then embedded in the .war file

As Andrew Logvinov says,
Look up hostname from Maven
Thanks :-)

Related

Providing system properties via command line with dot in the name in Gradle

We are migrating our project from Maven to Gradle. Our CI uses system properties like -Dwebdriver.type=firefox to set certain behaviour thus we don't want to hardcode such props in gradle.properties file etc. Is there a way to provide a system property with a dot in the name using command line?
If you run the following:
build.gradle:
logger.lifecycle("some.property ${System.properties['some.property']}")
with:
gradle -Dsome.property=lol
It should give you the expected output.

Is there a way to set the Maven version number dynamically?

I would like to use Maven to produce an artifact in zip format. To give you some background; my project includes an APS package (Application Packaging Standard, used to provision cloud applications on the Parallels platform). This package is a zip file that contains a combination of XML as well as PHP files. It is generated by an APS plugin from within Eclipse and its name always includes the version and release number of its contents.
What I am trying to do is generate a zip file with Maven that would be kind of a release candidate that will be eventually sent to customers and would include not only the actual APS package but also other files such as README, User Guide.pdf, etc;. I would like the name of this zip file to contain the version number of the version number of the APS package. Currently I can generate this manually by using something like "mvn -Dversion=1.2.3-4 package" but I would like to automate the process and ideally run this from Jenkins.
Basically, my strategy is to run a script that would extract the version number from the initial APS package, once that is done, my script can invoke Maven and can pass this parameter to it so it can generate the final zip with the proper version number. This is fine but again, I need to run this script manually and I am looking for an automated process.
My question is; is it possible to invoke this script from within Maven and use its return as a parameter to set the version name (or the name of the file that will be generated) at run time? As I mentioned, I would like eventually Jenkins to handle this. It can pick up the pom file but I am not sure how it could kind of "auto configure" itself to have the proper version number.
Thanks is advance.
From jenkins build you can use profile with ${BUILD_NUMBER}:
<profile>
<id>jenkins</id>
<build>
<finalName>${artifactId}-${version}-${BUILD_NUMBER}</finalName>
</build>
</profile>
Then run in jenkins:
clean install -Pjenkins
I use the SVN (or any source versioning system) version to identify the software builds.
By simply executing this
REVISION=`svn info | grep '^Revision:' | sed -e 's/^Revision: //'`
on the sourcers folder you get the right value in $REVISION, then you can use it for your maven build
mvn -Dversion=1.2.3-$REVISION package
easy and clean

Ant set platforms.JDK_1.6.home without parameter

I'm having a problem trying to run ant on a windows machine. I get the following error:
BUILD FAILED
C:\Users\USER\testing\mercurial\project\NetbeansProject\nbproject\build-impl.xml:111: The J2SE Platform is not correctly set up.
Your active platform is: JDK_1.6, but the corresponding property "platforms.JDK_1.6.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.JDK_1.6.home" in a .properties file)
or ant -Dplatforms.JDK_1.6.home=<path_to_JDK_home> jar (where no properties file is used)
If I execute ant -Dplatforms.JDK_1.6.home=%JAVA_HOME% it executes fine, but, is there some way to avoid adding this parameter every time I need to build a program?
I don't think it's possible to set ant properties outside of command line or properties files loaded explicitly by the build script.
If you are looking for a less verbose way to launch ant, try using either a wrapper .bat file, or assigning an alias doskey ant=ant "-Dplatforms.JDK_1.6.home=%JAVA_HOME%"

Manipulating __artifactId__ during file generation by maven archetype

Building a Maven archetype where files are generated using _artifactId_. archetype.xml looks like:
<sources>
<source>src/main/java/__artifactId__.java</source>
<source>src/main/java/__artifactId__CommandExecutor.java</source>
<source>src/main/java/__artifactId__EventListener.java</source>
</sources>
Generating a project using this archetype can lead to Java file names that do not follow the naming convention such as sample-plugin.java and sample-pluginCommandExecutor.java.
How can I make sure _artifactId_ is converted to appropriate Java file name, such as SamplePlugin.java and SamplePluginCommandExecutor.java.
I'm not an archetype expert, in fact I just did it once but
faced with a very similar problem.
I was not able to find a way to customize file or directory names
in any way, I mean use one of the archetype parameters and transform
it.
So there are 2 kinds of solution I used
Add a new archetype parameter where you specify the class name
Use a standard Name (like ArtifactClass.java) and then create an maven profile (enabled if this file is present) with an ant script that changes the name (just remember inside your files you can use velocity to customize it)
Hope it helps
tonio

Activate a profile based on environment

Here's my scenario:
Maven 2.0.9 is our build system
We install code to multiple environments
All of our environment-specific properties are contained in property files, one for each environment
We currently read these properties into maven using the properties-maven-plugin; this sub-bullet is not a requirement, just our current solution
Goal:
Perform certain parts of the build (ie. plugin executions) only for certain environments
Control which parts are run by setting values in the environment-specific property files
What I've tried so far:
Maven allows plugins executions to be put inside pom profiles, which can be activated by properties; unfortunately these must be system properties - ie. from settings.xml or the command-line, not from properties loaded by the properties-maven-plugin
If possible, we'd like to keep everything encapsulated within the build workspace, which looks something like this:
project
pom.xml
src
...
conf
dev.properties
test.properties
prod.properties
build-scripts
build.groovy <-- the script that wraps maven to do the build
install.groovy <-- ... wraps maven to do the install
Running a build looks like:
cd build-scripts
./build.groovy
./install.groovy -e prod
Is there any possible way to accomplish these goals with the version of maven we are using? If not, is it possible with a newer version of maven?
This isn't possible using just Maven. (See also How to activate profile by means of maven property?) The reason is that profiles are the first thing evaluated before anything else to determine the effective POM.
My suggestion is to write some preprocessor that parses your environment specific property files and converts them to the required system properties before launching Maven. This script can be included in your ~/.mavenrc so that it runs automatically before Maven is launched. Here is an example script that that assumes the properties file is in a fixed location:
properties=`cat /etc/build-env.properties`
while read line; do
MAVEN_OPTS="$MAVEN_OPTS -D$line"
done <<< "$properties"
If the properties file is not fixed, you'll just need to add something to the script to discover the location (assuming it is discoverable).

Resources