How can a maven property be used in a maven attribute? - maven

For a previously defined property <scala.js.opt> I'd like to be able to include it like so in a pom.xml file:
<arg value="${scala.js.opt}"/>
However, this doesn't seem to work. Is it even possible, or are there any workarounds?
The particulars of my specific case is that these are arguments to an executable in the maven-antrun-plugin, should this be easier with my specific case.
Detailed example follows
In master pom file:
<properties>
<scala.js.opt>fastOptJS</scala.js.opt>
</properties>
Then in child pom file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<exec executable="cmd.exe"
spawn="true">
<arg value="/c"/>
<arg value="C:\Program Files (x86)\sbt\bin\sbt.bat"/>
<arg>${scala.js.opt}</arg>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
When run using e.g., mvn install, this produces:
org.apache.tools.ant.types.Commandline$Argument doesn't support nested text data ("fastOptJS")

Related

How to run powershell script in maven

I currently dealing with a problem using maven. I try to update a file when building the software using maven. For some reason, I have to use a powershell script and run it when building using mvn. Here is my code:
<exec executable="powershell.exe"
spawn="true">
<arg value="/c"/>
<arg value="myReplace.ps1"/>
<arg value="../../the/path/to/the/directory/" />
<arg value ="filename"/>
<arg value="value1" />
<arg value="value2" />
<arg value="value3" />
</exec>
It does not work as expected, can someone help me?
This message dates from a while but I went through the exercise in the last days. The key is to use the exec plugin as stated. Here is an example in a project's pom.xml:
The code below launches the code-signing on a PowerShell module script. Note that the command string must begin, for calling PowerShell functions from the command line, with & which is not properly digested by Maven. Escaping it does the trick. For more complex operations, call a PS script.
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>scripts-package</id>
<goals>
<goal>exec</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<!-- optional -->
<workingDirectory>/Temp</workingDirectory>
<arguments>
<argument>-command</argument>
<argument>"& { Set-AuthenticodeSignature '${project.build.directory}/Logging_Functions/*.psm1' #(Get-ChildItem ${project.build.certificate.path} -codesigning)[0]"}</argument>
</arguments>
</configuration>
</execution>
This avoids a bug with PowerShell hanging on StdIn.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<useMavenLogger>true</useMavenLogger>
</configuration>
<executions>
<execution>
<id>run-my-script</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>powershell.exe</executable>
<arguments>
<argument>-InputFormat</argument>
<argument>None</argument>
<argument>-File</argument>
<argument>${basedir}/my_script.ps1</argument>
</arguments>
</configuration>
</execution>
<executions>
<plugin>

How to call maven-antrun-plugin target without attach execution to a maven phase ?

I use maven-antrun-plugin for init config files for my project. But i need to init config files just once, when i first start to init my dev environment, not each time i launch jetty:run.
If I attach phase to process-resouces for example, each time I launch jetty, my config files are reseted.
So i configured antrun like this :
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="init_config_files">
<!-- init files -->
</target>
</configuration>
</execution>
</executions>
</plugin>
If I launch mvn antrun:run, it just return me this error : "[INFO] No ant target defined - SKIPPED". And it is the same thing, if I specify target : "mvn antrun:run -Dtarget=init_config_files".
Try this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath" />
<echo message="compile classpath: ${compile_classpath}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
and run this:
mvn antrun:run
Best solution I have found so far:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>go-live</id>
<configuration>
<target>
<!-- go live! -->
<exec executable="${basedir}/deploy2server.sh" failonerror="true" dir="${basedir}">
<arg value="deploy" />
<arg value="${deploy.to.server}" />
<arg value="${jetty.port.live}" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
and run this:
mvn antrun:run#go-live
This solution avoids the target being run by accident, ie. it does not run just by typing "mvn antrun:run" and it also does not run during regular maven runs. I'm using this for qa auto-deployment in my jenkins instance after all modules incl integration done against the final distribution package have been successfully executed.
I just ran into the same problem and finally figured it out: If you want to run the ant tasks only once, you can set the plugin up like this:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<target name="init_config_files">
<!-- init files -->
</target>
</configuration>
</plugin>
and use mvn antrun:run to execute it. That way, the ant stuff is not bound to any phase.
If you need to run some part of build on special conditions (e.g. once), you can put those parts into a Maven profile and then invoke Maven specifying profile name, e.g. mvn -p init_config_files package

How do I make this plugin run only on non-Windows platforms?

I'm using Maven 3.0.3. For our project integration testing, we require a virtual frame buffer to be created, using Unix commands. However, when we run our project on the Windows machines, we don't need this. We use
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>start-xvfb</id>
<phase>process-test-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Starting xvfb ..." />
<exec executable="Xvfb" spawn="true">
<arg value=":0.0" />
</exec>
</tasks>
</configuration>
</execution>
<execution>
<id>shutdown-xvfb</id>
<phase>post-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Ending xvfb ..." />
<exec executable="killall">
<arg value="Xvfb" />
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
How can I make the above run when the platform isn't windows and suppress the running of the plugin otherwise? Thanks, - Dave
You can do this with profiles. There is profile activation switch for OS settings. And the plugin is so intelligent, you can use negation.
<profiles>
<profile>
<activation>
<os>
<family>!windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
...
</plugin>
</plugins>
</build>
...
</profile>
</profiles>
Further information about profiles can be found here, and about the values you can use here.

Is it possible to run a Bash script from Maven?

For creating configuration of my application I need to run bash script.
Is it possible to integrate execution of Bash scripts in Maven, maybe there are some plugins?
Could the Bash Maven Plugin help you? (Disclaimer: I initiated it, so please send me feedback)
<build>
<plugins>
<plugin>
<!-- Run with:
mvn bash:run
mvn install
-->
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>bash-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>test</id>
<phase>integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<script>
# Here you can execute shell commands
echo "Tomcat will start"
/opt/apache-tomcat/bin/startup.sh
</script>
</configuration>
</plugin>
</plugins>
</build>
You will need to install this maven plugin in your own Maven repo.
Like Konstantin: When you execute a shell script, you're not portable anymore.
You can do this, see answer:
I want to execute shell commands from maven's pom.xml
But it is not advisable, as this produces not so portable builds. Why do you need this in first place? Using this plugin usually indicates some weird necessity in project build
Would look more like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>generateSources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec executable="/bin/bash">
<arg value="myFirst.sh" />
<arg value="inputOne" />
</exec>
<exec executable="/bin/bash">
<arg value="src/mySecond.sh" />
<arg value="inputTwo" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
With myFirst.sh:
echo "call to myFirst.sh, message ${1}"
Solved. The problem is, executable is working in a different way for bash. This code is working. Write it in pom.xml
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution><!-- Run our version calculation script -->
<id>Renaming build artifacts</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<commandlineArgs>handleResultJars.sh</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
If at all possible, I'd recommend using a scripting language that runs inside the JVM with dependencies that are captured either in your project or in the maven repository. That way your builds are platform independent and your dependencies are captured (i.e. you don't loose the build machine and realize your bash script was specific to that box). I showed an example in this post of using jacl. There are also good examples of using javascript and groovy inside antrun (though there may be more straightforward ways of calling them directly).
Use the maven-antrun-plugin artifact. This way, you can execute several executables sequentially more easily than exec-maven-plugin. Example:
* The <exec> tag is the important one here.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec executable="my1.sh">
<arg value="input1"/>
</exec>
<exec executable="my2.sh">
<arg value="input2"/>
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
To experiment with commands you can use exec:exec:
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="your arguments"
your arguments
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="'your arguments'"
your arguments
This demonstrates:
passing arguments: if you need to pass several arguments: just split them with space
if you need to pass an argument with spaces: enclose it in quotes, as you would do in bash script/terminal
-q to shut up the mvn log

How to use maven-antrun-plugin for phase exec

Is it possible to use maven-antrun-plugin for the exec phase/goal.
currently I use the following but it does not work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>exec</id>
<phase>exec</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target
name='Exec'
>
<exec
executable='${env.COMSPEC}'
osfamily='winnt'
>
<arg
value='/c'
></arg>
<arg
value='src\\main\\scripts\\Exec.cmd'
></arg>
</exec>
<exec
executable='src/main/scripts/Exec.command'
osfamily='unix'
></exec>
</target>
</configuration>
</execution>
</executions>
</plugin>
Of course if anybody knows a way around the ${env.COMSPEC} hack on windows that would be welcome as well.
It looks like the plugin is used to run an arbitrary executable and not an ant task which should ideally not have OS-specific check.
Perhaps you should look at Exec Maven Plugin?

Resources