Setting environment variable with maven 2.x - linux

Is it possible to set environment variable with maven (OS: Linux)?
I already have user-defined properties (in the pom and in profiles.xml)....my problem is, how to execute following from Maven
export GGA_FRE=/path
So will be possible, that every developer can set his own path for the GGA_FRE.

This answer is not correct, at least not completely (see comments).
Unfortunately I can't delete it as it has been accepted. Your milage may vary.
Use the exec:exec mojo.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exportVar</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>export</executable>
<arguments>
<argument>GGA_FRE=${my.path}</argument>
</arguments>
</configuration>
</plugin>
now call it like this mvn install -Dmy.path=/var/users/groucho

I don't think there is a Java way to set environment variable the way export command does (so that it is avaliable outside of Java). (see for example this question: How do I set environment variables from Java?)
However, you might hack you way around: for example use maven-exec plugin to run a shell script and then set the variable in the script. You might pass a parameter to your script to specify the variable value.
(note that I have not tested this)

Related

how does the ant use in groovy

this is a groovy script file MavenInit.groovy, execute in a pom.xml as follow, the var 'SCRIPTS_GROOVY' is defined in another pom.xml,what confuse me is I can not find where the "DIR_TARGET" definition, and how can use ant direct without ant = new AntBuilder()
SCRIPTS_GROOVY=project.properties['SCRIPTS_GROOVY']
ant.pathconvert(targetos:"unix", property:"DIR_TARGET") {
path(location:project.build.directory)
}
DIR_TARGET=ant.project.properties['DIR_TARGET']
project.properties.setProperty('DIR_TARGET', "${DIR_TARGET}")
project.properties.setProperty('DIR_LOGS', "${DIR_TARGET}/logs")
ant.mkdir(dir:"${DIR_TARGET}/logs")
the pom.xml excute the MavenInit.groovy as follow
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<executions>
<execution>
<id>set-maven-property</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>file:///${SCRIPTS_GROOVY}/MavenInit.groovy</script>
</scripts>
</configuration>
</execution>
<execution>
<id>Windows-package</id>
<phase>compile</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>file:///${SCRIPTS_GROOVY}/PackageWindows.groovy</script>
</scripts>
</configuration>
</execution>
</executions>
</plugin>
how can I find the 'DIR_TARGET' initial defined and change the value
GMavenplus plugin set an ant property into its script
if (!properties.containsKey("ant")) {
try {
Object antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder")));
properties.put("ant", antBuilder);
}
Also there are other properties like project, log, etc.
About DIR_TARGET, I think it is the value of project.build.directory with path separator normalized as unix like, from path(location: project.build.directory).
(I am not an ant expert, but I test on my computer and these two have same value.
Just adding the log lines to see
log.info( project.build.directory)
DIR_TARGET = ant.project.properties['DIR_TARGET']
log.info(DIR_TARGET)
I searched the ant doc and find an example from https://ant.apache.org/manual/Tasks/pathconvert.html, which verifies my guess.
<pathconvert property="prop" dirsep="|">
<map from="${basedir}/abc/" to=''/>
<path location="abc/def/ghi"/>
</pathconvert>

Hudson Config for Mule (Cloudhub)

I am setting up a job in hudson to build maven based mule application on SVN , uploading to artifactory and then deploy it on cloudhub.
I am able to build project and upload it to artifactory but the problem is how to deploy it on cloudhub after that.
I have groovy post build plugin but not sure what script to write in it to proceed.
Is there anyone who could give me some pointers to proceed??
Thanks in advance
You should use mule-maven-plugin, it is the currently supported way to deploy to CloudHub via Maven. This a sample plugin configuration:
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<deploymentType>cloudhub</deploymentType>
<muleVersion>3.7.0</muleVersion> <!-- This is the runtime version as it appears on the CloudHub interface -->
<username>myUsername</username>
<password>myPassword</password>
<environment>Production</environment>
</configuration>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
And remember to add this to your settings.xml so Maven can find the plugin:
<pluginRepositories>
<pluginRepository>
<id>mule-public</id>
<url>https://repository.mulesoft.org/nexus/content/repositories/releases</url>
</pluginRepository>
</pluginRepositories>
You can use maven cloudhub-maven-plugin. Please refer details at cloudhub-maven-plugin
Hope this helps.

How to display the output of the exec-maven-plugin instantaneously

I'm using Maven 3.1.1 and the exec-maven-plugin (1.3) in order to execute a bash script during a build job.
The bash script produces output on stdout with echo and printf. I've noticed that the output of the script is not written to the maven console output instantaneously. Instead the maven console output "freezes" until it gets updated with multiple output lines of the bash script at once. I don't know what's the trigger for an update of the maven output (timeout? full output buffer?) but it's very slow.
Let's take a very simple bash script, e.g. counter.sh:
#!/usr/bin/env bash
for i in `seq 1 1000`; do
echo $i
sleep 0.5
done
And here's my plugin configuration in the pom.xml:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.3</version>
<executions>
<execution>
<id>execute-script</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.build.directory}/executable/counter.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
When I execute the build job with mvn clean package, the maven output freezes at the exec-maven-plugin and shows no progress/output until the script has completed after ~8 minutes.
When I execute another script that is running even longer, I get a block of output each ~15 minutes.
What I'm looking for is a way to see the output of the bash script instantaneously in the maven console output.
Update: Solution using maven-antrun-plugin (thanks to Ivan)
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>execute-script</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<exec dir="${project.basedir}" executable="${project.build.directory}/executable/counter.sh" />
</target>
</configuration>
</execution>
</executions>
</plugin>
JFYI, the issue is fixed in exec-maven-plugin v 1.3.2 - http://blog.soebes.de/blog/2014/07/28/mojo-exec-maven-plugin-version-1-dot-3-2-released/
The exec-maven-plugin uses an output stream that does not flush automatically.
I think you have two options:
Copy and change the exec-maven-plugin to suit your needs.
Use antrun plugin with ants exec task. This does flush the output stream so you can see the output when it comes. See http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/
Maybe the 2nd option could be slower, since maven calls ant, and ant then calls your script, but is pretty easy.
As Ivan already pointed out exec-maven-plugin:1.3 doesn't automatically flush the output stream. This can lead to delayed output.
I observed that this behaviour is only present in the plugin from version 1.3 onward.
exec-maven-plugin:1.2.1 actually has the desired behaviour and flushes the output stream. So instead of using ant you could switch to an older version of exec-maven-plugin.

appassembler maven plugin doesn't set "execute" permissions on generated script

The AppAssembler Maven plugin does a great job of generating distribution for me. One last problem is that the generated Shell script does not have execution permissions so I need to set them manually.
I am on Linux RedHat
Does anybody know of a clean way to set them automatically?
The only way to do this is to process the file with another maven plugin like Antrun or Assembly after running AppAssembler.
This issue (see link below) has been brought up on the AppAssembler project issue tracker and it was rejected as Won't Fix.
Issue: MAPPASM-54
I think it can be set in your assembly.xml, in the fileSet tag:
<fileSets>
<fileSet>
<directory>src/resources/bin</directory>
<lineEnding>keep</lineEnding>
<useDefaultExcludes>true</useDefaultExcludes>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.bat</include>
<include>*.sh</include>
</includes>
<fileMode>744</fileMode>
</fileSet>
...
Since Maven 3.0.3 all plugins are executed in the order they are in your pom.xml. So setting the executeable flag in a platform independet manner is as easy as using the maven-enforcer-plugin right after your appassembler plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-beanshell</id>
<phase>package</phase>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<evaluateBeanshell>
<condition>
import java.io.File;
print("set executable for file ${basedir}/dist/bin/mql");
new File("${basedir}/dist/bin/mql").setExecutable(true,false);
true;
</condition>
</evaluateBeanshell>
</rules>
<fail>false</fail>
</configuration>
</execution>
</executions>
</plugin>

Where to get a full list of pre-defined variables in gmaven-plugin?

Where to get a complete list of variables available in Groovy scripts executed under gmaven-plugin in Maven? Besides that, maybe someone knows where to find Gmaven documentation?
I'm aware about project and settings. I assume there are some others..
The page http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code lists:
Default Variables
By default a few variables are bound into the scripts environment:
project The maven project, with auto-resolving properties
pom Alias for project
session The executing MavenSession
settings The executing Settings
log A SLF4J Logger instance
ant An AntBuilder instance for easy access to Ant tasks
fail() A helper to throw MojoExecutionException
This snippet in your pom should give you a better idea of what's available while running the script. Most of the interesting bits are probably in the binding.project, an instance of MavenProject.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<properties>
<hello>world</hello>
</properties>
<source>
println this.binding.variables
println project.properties
println settings.properties
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources