What does "echo" mean when it comes to using Maven? - maven

I'm currently learning how to use Maven and have encountered a term called "echo". I was just wondering what it actually means?
There are few important concepts related to Maven Lifecycles, which
are worth to mention:
1) When a phase is called via Maven command, for example mvn compile,
only phases up to and including that phase will execute.
2) Different maven goals will be bound to different phases of Maven
lifecycle depending upon the type of packaging (JAR / WAR / EAR).
In the following example, we will attach maven-antrun-plugin:run goal
to few of the phases of Build lifecycle. This will allow us to echo
text messages displaying the phases of the lifecycle.

echo is an ant task which allow to print messages to console (system.out)
This makes sense when using maven-antrun-plugin which allow to execute ant tasks in a maven build.
It can be used to print some maven properties during the build as there is no built-in way to output value to console in maven.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.plugin.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Configuration properties :</echo>
<echo>service.endpoint=${service.endpoint}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

It means to use echo tag in ant script executed by maven-antrun-plugin:run. Nothing directly related to maven itself.

Related

How to run script in mvn as test

I would like to have the following scenario in Maven/Jenkins:
Run test scripts (bash/shell)
when the script exited with a problem (an error), then the Maven build on Jenkins should be on UNSTABLE and not FAILURE status
Question: How can I do it?
You can run scripts in Maven using the Exec Maven Plugin and its exec goal.
If you want to run the script during the test phase, then you can bind an execution of the plugin to it as following:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>run-test-script</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable><!-- configure here your script .sh/.cmd --> </executable>
<arguments>
<argument><!-- configure here arguments, if any --></argument>
</arguments>
<workingDirectory><!-- configure here PWD, if required --></workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
Note that you can also configure different successful exit codes via the successCodes configuration entry.
If the script fails, then the build will fail. However, you can change this behavior on the Jenkins build via the Jenkins Text Finder Plugin and configure it as Post-build Action:
You should set-up a regular expression which could be found as part of the Maven build output on Jenkins. As example, the regex .*Script Failed.* would match the string Script Failed printed by the script in such a case. So the build will actually fail, however we can change its status on Jenkins (but not on Maven)
You should check the option Unstable if found which will convert the status of the build from FAILED to UNSTABLE
As per documentation of the Unstable if found option:
Use this option to set build unstable instead of failing the build.
You can see an example of such a configuration in the image below:
As such, you would have a script executed in the test phase as you desired, the Maven build would fail if the script did so but the Jenkins build would change its status according to your configuration of the Text Finder plugin.
Also note: if you want Maven not to fail in case the script did, you can play with the successCodes as mentioned above and still make the Jenkins build change its status to UNSTABLE according to the same configuration of the Text Finder plugin. Hence different combinations are possible.

Use maven-exec-plugin to run command line

I want to use maven-exec-plugin to run command line (cmd) for converting a Markdown file to a PDF file using Pandoc.
To do that manually, I've executed these commands:
pandoc ReadMe.md -o ReadMe.html
pandoc ReadMe.html --latex-engine=xelatex -o ReadMe.pdf
I wasn't able to run that in one command, pandoc giving weird error! But this is another problem...
I've added this to my pom file using other sample found on the web but without success.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>pandoc</id>
<phase>generate-pdf</phase>
<configuration>
<executable>cmd</executable>
<workingDirectory></workingDirectory>
<arguments>
<argument>/C</argument>
<argument>pandoc</argument>
<argument>README.md</argument>
<argument>-o</argument>
<argument>README.html</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'm not a maven guru and help is appreciate!
The defined phase, <phase>generate-pdf</phase>, is not a maven phase, hence Maven didn't bind the execution to its workflow.
You should bind it to a standard Maven phase, depending on your need. Try <phase>package</phase> for instance, it will be executed nearly at the end of your build.
The id element of a plugin execution is free text, you can type the id you want, it will appear as part of the build output in curved brackets after the plugin and goal name,
i.e. exec-maven-plugin:1.1:exec (pandoc)
The phase element instead must match a well known maven phase in order to attach the plugin/goal execution to it. If the phase is not well known, then Maven will simply ignore that plugin/goal execution (which is also an adopted approach, usually using the de-facto standard none as phase, to disable an inherited plugin execution, but that's a bit advanced for the scope of this question I would say).
For more details on maven phases, look at the official documentation, here.
For a full list of maven phases, here.

Incomplete WAR automatically uploaded by Maven

To ease up the deployment process of my Jave EE application, I instructed Maven to automatically copy the resulting WAR file to the application server.
pom.xml:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution><!-- Run our version calculation script -->
<id>Copy to Application Server</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/copy-to-appserver.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
copy-to-appserver.sh:
scp /home/user/.m2/repository/com/wolf/apix/1.0/apix-1.0.war user#srv-web:/opt/wildfly-8.2.0.Final/standalone/deployments/apix.war
Unfortunately, this fails! The WAR is successfully transmitted to the application server, but it's mixed with old and new code. My assumption is that Maven tries to send it while still being in the WAR creation process, because when I run the copy script copy-to-appserver.sh manually after the deployment, everything works fine with it on the application server.
My question is, what do I have to change, so that Maven only accesses the WAR file when its creation / manipulation is complete?
Your plugin is being executed prematurely in the generate-sources phase
Run it in the last phase by changing the phase to deploy
<phase>deploy</phase>
In addition running the plugin in the correct phase, as suggested by 6ton, you might also want to consider using the Maven WildFly plugin, which specifically designed to solve your problem. That way, you can get rid of that nasty, nasty script.
I would recommend to separate the build process and the deployment process cause the deploy life cycle phase is intended to upload the artifacts to a remote repository.

Maven: Bind plugin execution to the execution of another plugin, not to a lifecycle phase

Note regarding the accepted answer: I accepted the answer because of strong circumstantial evidence. Nonetheless, this is circumstantial evidence, so take it with a grain of salt.
How can I have a plugin be triggered when the user runs a plugin goal, not a lifecycle phase? (This has been asked before, but the answer was to use a lifecycle phase.)
Case in point: I need release:branch to invoke regex-plugin to generate a branch with the current version as its name, minus the -SNAPSHOT suffix. This is what I have, which requires the developer to activate a profile and invoke the verify phase. I need the developer to simply invoke release:branch, which in turn should cause regex-plugin to run. In a bit of a marriage to Gitflow.
<profile>
<id>Release Branch</id>
<build>
<plugins>
<!-- On validate, compute the current version without -SNAPSHOT. -->
<!-- Put the result in a property. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<value>${project.version}</value>
<regex>^(.*)-SNAPSHOT$</regex>
<replacement>$1</replacement>
<name>project.unqualifiedVersion</name>
</configuration>
</execution>
</executions>
</plugin>
<!-- Also on validate, run the branch plugin, and use -->
<!-- the non-SNAPSHOT version thus computed in the branch name. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>branch</goal>
</goals>
<configuration>
<branchName>release/${project.unqualifiedVersion}</branchName>
<updateWorkingCopyVersions>true</updateWorkingCopyVersions>
<updateBranchVersions>false</updateBranchVersions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
The intent is for release:branch to move the current snapshot version (say, 1.0.5-SNAPSHOT) into a new branch, which should be named after the version but without the superfluous -SNAPSHOT suffix (1.0.5). The current branch should then take on a new snapshot version (1.1.0-SNAPSHOT, not 1.0.6-SNAPSHOT, because we want release 1.0.x to have room for hotfixes, so we reserve it for the branch) (I don't have the automatic computation of the next snapshot version figured out yet, so, if you run the Maven configuration above with validate, you will have to enter it at a prompt).
The evidence presented so far is rather circumstantial. I've done some research of my own, so it's best I share it here. The below are either more of the same "it's not possible", or the building blocks for alternatives.
jetspeed:mvn plugin --- run a specified sequence of plugins; the configuration to run can be varied via a system property; IDE integration concerns
Executing goals before plugin runs (StackOverflow) --- same question answered in the context of a custom Mojo
Make Mojo run other goals (StackOverflow) --- again, from the context of a custom Mojo
Configuring default Mojo executions --- Maven page describing how Mojos run - more circumstantial evidence
Triggering phases before goal execution (StackOverflow) --- roundabout solution to my problem, unfortunately answered in the negative
INTERESTING: Guide to Ant plugin development --- appealing to me, because, while it requires writing a custom plugin, it's all Ant + Maven configuration, no code to compile; presumably a lower barrier to entry
Creating a parallel lifecycle --- appealing approach, because I could fully control the contents of the lifecycle to where it would use Gitflow verbs; unclear how IDEs would integrate this; learning curve and adoption barrier concerns exist
No, you can't bind to a plugin to another plugin. Only to a phase.
In Maven-internal terms, a "Mojo" is the thing that does work. A "plugin" is a collection of mojos wrapped up so you can reference them from the POM. Mojos bind to phases only.
From the plugin development documentation:
Each Mojo specified inside a plugin descriptor must provide the following
...
phase ... Defines a default phase to bind a mojo execution to if the user does not explicitly set a phase in the POM. Note: This annotation will not automagically make a mojo run when the plugin declaration is added to the POM. It merely enables the user to omit the <phase> element from the surrounding <execution> element.
For further confirmation, see the source of MojoExecution (the JavaDoc for this class isn't helpful) and notice that there are two possible sources of execution enumerated:
An execution that originates from the direct invocation of a goal from the CLI
and
An execution that originates from a goal bound to a lifecycle phase
No other way to kick off an execution means you're out of luck (barring extraordinary measures like rolling your own plugin that combines the effects of the two plugins you want to link and then using your custom plugin).

Maven generate-source ALWAYS Successful

I am trying to run a ant task in maven generate-sources phase.
However, after many non productive "successes" i've realised, that the build always succeeds regardless of anything i enter.
Here is the plugin config for the pom.xml of my module.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<configuration>
<tasks>
<fail message="Something wrong here."/>
</tasks>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
It still succeeds. If i place a bad ant file. Still succeeds.
Does anyone know what i could be doing wrong here?
The Compile/Clean/Install/Deploy phases all work fine. Just "generate-source" doesnt work at all.
My settings.xml file only contains Repo information
Thanks for any advice
Edit:
I've been able to narrow the error down a little.
<configuration>
<target>
<echo message="hello ant, from Maven!"/>
<echo>Maybe this will work?</echo>
</target>
</configuration>
If place that within the configuration of the plugin, Not in the nested configuration in the execution tag. and run "mvn antrun:run" I see the echos. However If i place it in the nested configuration in the execution element, it doesnt show... Is there some link missing between my mvn goal and the antrun instruction?
I don't get it. The same configuration runs outside of the executions/execution tags, but not within.
Solution *Solution* Solution
AHHH I found it!
In my pom.xml I had all my 'plugins''plugin' configured under the 'pluginManagement' .. This 'pluginManagement' configures the plugins not for this project, but for all children project. Effectively a parent default config file for all children implementing the plugin.. I simply removed the 'pluginManagement' tags and it works...
Thank heavens. I've been looking at this for a full day...
Try adding <failOnError>false</failOnError> to the execution.
Per the documentation, this parameter "specifies whether a failure in the ant build leads to a failure of the Maven build. If this value is 'true', the Maven build will proceed even if the ant build fails. If it is 'false', then the Maven build fails if the ant build fails."
This is counterintuitive to me. I would think that the default value of "true" would cause the Maven build to fail if the ant build failed, but that isn't what the docs appear to be saying.

Resources