how does the ant use in groovy - 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>

Related

Using maven jaxb2 plugin, modular compilation using episode and catalog throws Malformed URL error

My project contains A.xsd which imports schema as follows from B.xsd which is part of another project:
<xsd:import namespace="http://com.test.schema/common/Context" schemaLocation="http://com.test.schema/common/Context/B.xsd"/>
I am trying to use the episode from the project which contains B.xsd so that classes related to B.xsd do not get re-generated when A.xsd is parsed. So I referred this and this to come up with the following configuration:
Here is the pom.xml
<dependencies>
<dependency>
<groupId>com.bar.foo</groupId>
<artifactId>schema-b</artifactId>
<version>1.2</version>
</dependency>
<dependencies>
.
.
.
.
.
<build>
<plugins>
<dependency>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<extension>true</extension>
<episodes>
<episode>
<groupId>com.bar.foo</groupId>
<artifactId>schema-b</artifactId>
</episode>
</episodes>
<catalog>src/main/resources/catalog.cat</catalog>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/schemas</directory>
<includes>
<include>A.xsd</include>
<include>...</include>
<include>...</include>
</includes>
</fileset>
</schema>
</schemas>
<bindingDirectory>${basedir}/src/main/schemas</bindingDirectory>
<bindingIncludes>
<include>*.xjb</include>
</bindingIncludes>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.0</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.0</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is the catalog file:
PUBLIC "http://com.test.schema/common/Context" "maven:com.bar.foo:schema-a:jar::1.2!"
There is some configuration in the xjb file to make sure that XmlRootElement is written into some generated classes:
<jxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.1" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:annox="http://annox.dev.java.net" extensionBindingPrefixes="xjc">
<jxb:globalBindings>
<xjc:simple />
</jxb:globalBindings>
<jxb:bindings
schemaLocation="A.xsd">
<jxb:bindings node="//xsd:complexType[#name='ADataType']">
<jxb:class name="AData" />
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
name="AData" />
</annox:annotate>
</jxb:bindings>
</jxb:bindings>
Inspite of providing the episode to the xjc execution and the location of the schema for B.xsd in the catalog file, the classes for B.xsd is getting generated.
The issue is that the maven artifact referred to by the catalog file is not being picked up. I see the following error in the maven build logs:
Malformed URL on system identifier: maven:com.bar.foo:schema-b:jar::1.2!
PUBLIC: http://com.test.schema/common/Context
maven:com.bar.foo:schema-a:jar::1.2!
Can anyone help tell me why am I hitting this malformed URL error for the artifact that contains B.xsd? Any help will be really appreciated.
Disclaimer: I'm the author of the maven-jaxb2-plugin.
First, you're probably mixing A and B here. You're saying A imports B but then you're using the schema-a artifact as episode. If B is imported, you should use schema-b as episode to not regenerate B stuff when compiling A.
But I think this is probably just a minor mistake in the question.
You have two aspects here - episodes and catalogs.
Episodes allow you to skip generation of classes you've generated somewhere else already. So if you use schema-b artifact when compiling schema-a then XJC should not generate classes for schema-b. You don't need catalogs for that, it's independent.
Sometimes XJC still generates few leftovers - even if you use an episode. I often get ObjectFactory and maybe some enums or top-level-elements generated. I believe this is an issue in XJC, there's nothing I can do in the maven-jaxb2-plugin about it.
So as a workaround I just use maven-antrun-plugin to delete unnecessary generated things.
If you get all of the B stuff generated then you should check if schema-b artifact really have the episode file generated. Check if you have META-INF/sun-jaxb.episode inside the JAR. See this answer for some trivia on the episode file.
So if you correctly configure the correct episode artifact you should not get B things generated, you don't need catalogs for this.
What you need the catalog for is to avoid downloading http://com.test.schema/common/Context/B.xsd when compiling. You can use catalogs to point to anothe location. I think your problem here is that you refer http://com.test.schema/common/Context to maven:com.bar.foo:schema-a:jar::1.2! which obviously does not point to the schema resource.
If you have an import like
<xsd:import namespace="http://com.test.schema/common/Context" schemaLocation="http://com.test.schema/common/Context/B.xsd"/>
Then you should probably rewrite it as follows:
PUBLIC "http://com.test.schema/common/Context" "maven:com.bar.foo:schema-b:jar::1.2!/common/Context/B.xsd"
Assuming schema-b artifact contains your schema under /common/Context/B.xsd. Note that it maps namespace, not schema location.
You can also use REWRITE_SYSTEM to rewrite schema location. For example:
REWRITE_SYSTEM "http://com.test.schema" "maven:com.bar.foo:schema-b:jar::1.2!"
If you have an URL like http://com.test.schema/common/Context/B.xsd, it will be rewritten to maven:com.bar.foo:schema-b:jar::1.2!/common/Context/B.xsd. This will point to the resource /common/Context/B.xsd inside your schema-b JAR.
Another hint - if you use schema-b as dependency in your project, you can omit the version.
Here's an example of catalog from a real world project:
https://github.com/highsource/ogc-schemas/blob/master/schemas/src/main/resources/ogc/catalog.cat
It contains rewrites like:
REWRITE_SYSTEM "http://schemas.opengis.net" "maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc"

How to set up JASMINE using MAVEN-PLUGIN?

I have a folder structure like
-root
-pom.xml
-service
-web
-pom.xml
-src
-main
-test
-java
-javascript
-lib
-specRunner.js
-runner.html
-spec
-model
-view
-collection
I have written JASMINE test cases in the "spec" folder containing "model","views","collections".
I'm able to see my test results in runner.html.
Now i'm trying to integrate with MAVEN build. SO followed steps given in http://searls.github.io/jasmine-maven-plugin
But i'm getting build failure when doing mvn clean install and unable to see anything on localhost:8234 when doing mvn jasmine:bdd
I'm unable to find out what went wrong. The errors in console show something about require js.
Following is my POM.xml for jasmine-maven plugin
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.3.1.5</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>${project.basedir}/src/main/webapp/js</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript</jsTestSrcDir>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<preloadSources>
<source>${project.basedir}/src/main/webapp/js/libs/require.js</source>
</preloadSources>
</configuration>
</plugin>
Can anybody guide what is wrong or better provide some demo project (Backbone+Require+Jasmine) which is integrated with MAVEN build.
Thanks in Advance.
I read some blogs and tried editing my jasmine-maven plugin as follows
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.3.1.5</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
<configuration>
<debug>true</debug>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<sourceExcludes>
<exclude>lib/jasmine.js</exclude>
<exclude>lib/jasmine-html.js</exclude>
<!--<exclude>libs/text.js</exclude>-->
</sourceExcludes>
<preloadSources>
<source>${basedir}/src/main/webapp/js/libs/require.js</source>
<source>${basedir}/src/main/webapp/js/main.js</source>
</preloadSources>
<jsSrcDir>${basedir}/src/main/webapp/js</jsSrcDir>
<sourceIncludes>
<include>${basedir}/src/main/webapp/jslibs/*.js</include>
<!--<include>libs/jquery-1.9.1.js</include>
<include>libs/underscore-min.js</include>
<include>libs/backbone-min.js</include>
<include>libs/text.js</include>-->
<!--<include>../templates/*.tmpl</include>-->
</sourceIncludes>
<haltOnFailure>true</haltOnFailure>
<jsTestSrcDir>${basedir}/src/test/javascript</jsTestSrcDir>
<serverPort>8234</serverPort>
<specIncludes>
<include>spec/**/*.js</include>
</specIncludes>
<!--<specRunnerHtmlFileName>runner.html</specRunnerHtmlFileName>-->
</configuration>
</execution>
</executions>
</plugin>
Now i'm able to check the JASMINE specs in the console while mvn clean install as follows-
Caused by: net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: Error: src/views/../../templates/menu.tmpl HTTP status: 404
I'm getting errors on template files (exceptions) not sure what this is about.
Any help on this?
Thanks.

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>

Setting environment variable with maven 2.x

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)

Resources