Maven store MD5 checksum of file and file size in properties then filter another file - maven

I am wondering if there is a way in Maven to calculate the MD5 checksum and size of a file, put them into properties then use those properties to filter (text replace) parameters in another file. I am trying to generate a config file for Advanced Installer before I run it.

After spending a while googling for ways to do it with Maven I decided to look into using the antrun plugin. I googled for both features and the first link of both solved the problem. Seems like antrun is a great way to script most things in Maven.
My antrun configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<configuration>
<exportAntProperties>true</exportAntProperties>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="my_path" value="some path"/>
<length file="${my_path}" property="file.size"/>
<checksum file="${my_path}" property="file.md5"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
Maven resources plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>file1</include>
<include>file2</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
Which works great from the command line but for some reason the properties are not resolved in Intellij. I've posted another question for that.

Related

reading properties file into maven pom.xml not working

I need to use values from properties file in maven pom.xml, so i used properties-maven-plugin to read my properties file as follows
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
qura.properties file contains something like this..
config.file.path = resources/python/config/test.py
I need use this config.file.path variable in resource element of pom.xml
pom.xml
<resources>
<resource>
<directory>${basedir}/multilang/</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
<resources>
But the value for ${config.file.path} is not taking up from qura.properties file and I couldn't find test.py file in jar.
what I'm doing wrong in this code?
Thanks in Advance
try using version 1.0.0 and removing spaces around equal sign in properties file.
eg:
key=value
IMO, it does not matter whether you put the spaces or not around equal sign in properties file. You may need to check if the ${config.file.path} exists in the directory specified by ${basedir}/multilang
The below snippet works for me.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
</resources>
</configuration>
<inherited></inherited>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Build helper Maven Plugin property not working for resource targetPath

I'm using the Build Helper Maven Plugin to construct a property that is the version with "-" for "."... I have it configured as follows:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>webscript.version</name>
<value>${project.version}</value>
<regex>\.</regex>
<replacement>-</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
This works fine when I'm building resources that contain the ${webscript.version} property (they are correctly substituted as expected within the file), so this is working:
<resource>
<targetPath>./alfresco/site-webscripts/org/alfresco/aikau/${project.version}/webscripts</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/webscripts</directory>
</resource>
However, the problem I'm having is in using the property anywhere else within the project... what I want to do is to use the property for a target folder, like this:
<resource>
<targetPath>./alfresco/site-webscripts/customizations/${webscript.version}</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/extension-webscripts</directory>
</resource>
With this code, the folder created is simply "${webscript.version}" and not "1-0-66" (in the case where the current version is 1.0.66).
Both the working and not-working examples are in the same <build> element and so I'm assuming are in the same phase.
Can someone advise me on how I can adjust the configuration to get this to work, or to suggest an alternative approach to replacing "." with "-" as a new property that will work in this case?
I was able to work around this using the maven-antrun-plugin with the following execution:
<execution>
<id>rename-extensions-folder</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>Renaming folder</echo>
<move file="${project.build.outputDirectory}/alfresco/site-webscripts/customizations/${project.version}"
tofile="${project.build.outputDirectory}/alfresco/site-webscripts/customizations/${webscript.version}"/>
</target>
</configuration>
</execution>
...although I'd still really like to know how to make use of the custom property without needing this step!
you may use gmaven-plugin is groovy maven plugin is really powerful
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>change-properties</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
if (something) {
project.properties.myProperty = 'myPropertyValue'
}
</source>
</configuration>
</execution>
</executions>
here another Example
hope this helps

Move a config file into etc folder(of Karaf) when a (Maven)bundle is deployed

I want to move a cfg file into the etc folder of karaf whenever a bundle is deployed.
the cfg file is in under src/main/resource .i tried the following in the pom but its not working.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Using env.test.properties</echo>
<copy file="src/main/resources/test.cfg" tofile="${env.KARAF_HOME}/etc/test.cfg"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
How can i do it ?
One of the solution could be:
- put your test.cfg file in a more specific folder. (eg: src/main/resources/cfg)
- use the maven resources plugin
This is a working example based on the maven phase generate-resources (replace that phase by deploy in your case):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-to-karaf</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources/cfg</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>D:\apache-karaf-3.0.1\etc\</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Maven: use jar from URL as a source for resources

Dealing with a legacy project, I have the need to load text resources from a jar at an URL.
The text resources will be then filtered and included in the output; those resources come from a released artifact.
From resource-plugin I see it is only possible to give a number of directories; would it be possible to load resources as I need?
I want to do somthing like this, but using a remote jar instead of the oher project in the workspace:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<directory>../<another project on the same workspace>/src/main/filtered-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Remote resource plugin, as suggested in one of the answer doesn't work because no file from the imported bundle ends up in target; there is no way I can produce the original bundle using remote resource plugin (it's a legacy projetc still in use and completely out of my control).
I think the Maven Remote Resources Plugin will suit your needs.
EDIT:
Snippet obtained from the usage page of the plugin. That XML fragment will attach the plugin to the generate-sources phase (choose a different one if it doesn't fit your needs), will download the apache-jar-resource-bundle artifact and uncompress its contents into ${project.build.directory}/maven-shared-archive-resources.
For better results is recommended that the resources artifact had been created using the bundle goal of the same plugin.
<!-- Turn this into a lifecycle -->
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>process-remote-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>org.apache:apache-jar-resource-bundle:1.0</resourceBundle>
</resourceBundles>
</configuration>
</execution>
</executions>
</plugin>
EDIT 2: Alternative Solution using AntRun
If your artifacts don't suit Maven needs and you need something more customized, then using AntRun plugin you could get it somehow:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>download-remote-resources</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<get src="URL of the resource" dest="${project.build.directory}" />
<unzip src="${project.build.directory}/filename.[jar|zip|war]" dest="${project.build.directory}/${project.build.finalName}" />
</target>
</configuration>
</execution>
</executions>
</plugin>

Build multiple artifacts with different classifiers at once

W want my maven project to produce three artifacts with different classifiers at once. I know that I can produce it with modules etc. This is actually a resources project that I want to produce configuration for DEV, STAGE and PROD environment.
What I want to have is to run mvn:install once and have my.group:resources:1.0:dev, my.group:resources:1.0:stage and my.group:resources:1.0:prod in my repo.
This can be done without profiles if you specify multiple plugin executions and resource filtering.
Create a properties file for each version in ${basedir}/src/main/filters (e.g. prod.properties, dev.properties) holding appropriate values for each environment.
Turn on filtering for your resources:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Now add the resource plugin executions. Note the different filter file and output directory.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/dev</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/dev.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>prod</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/prod</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/prod.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Finally, the jar plugin; note classifier and input directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
<classesDirectory>${project.build.outputDirectory}/dev</classesDirectory>
</configuration>
</execution>
<execution>
<id>jar-prod</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<classesDirectory>${project.build.outputDirectory}/prod</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
Running mvn clean install should produce the properly filtered resources in artifacts with dev and prod classifiers like you want.
In the example, I used execution IDs of default-resources and default-jar for the dev versions. Without this you would also get an unclassified jar artifact when you build.
Just an FYI - put the version number in there to make sure you have the version supporting custom filters. In maven 3 I set mine up like this for example. Without version it didn't work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
...
</plugin>

Resources