Maven-Wagon - How to rename resources with special characters? - maven

I try to use the Mave-Wagon plugin to download a file via HTTP-GET:
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http</artifactId>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<id>download-zip</id>
<phase>generate-sources</phase>
<goals>
<goal>download-single</goal>
</goals>
<configuration>
<url>http://server.com/Download/</url>
<fromFile>FileDownload.aspx?param=asdf&param2=1234</fromFile>
<toDir>${project.build.directory}</toDir>
<toFile>targetfilename.zip</toFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem is the ? in the target filename as the toFile is applied only after the download happened.
When I download the URL via a normal browser, the browser gives me also a "clean" and nice file name. Is this HTTP-Header ignored by Wagon-HTTP?
Is there any way to pass the parameters to the URL or make Maven to use another/cleaned file name?
For completeness, the whole exception is
Caused by: java.io.FileNotFoundException: Illegal Syntax in filename (sorry this is an translation)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
at org.apache.maven.wagon.LazyFileOutputStream.initialize(LazyFileOutputStream.java:154)
at org.apache.maven.wagon.LazyFileOutputStream.write(LazyFileOutputStream.java:126)
at org.apache.maven.wagon.AbstractWagon.transfer(AbstractWagon.java:492)
at org.apache.maven.wagon.AbstractWagon.getTransfer(AbstractWagon.java:328)
... 25 more

The Wagon plugin exposes the Wagon API, and the Wagon API is not a general purpose remote file system access API. It's intended only to push content in and out of Maven repositories, not arbitrary files to arbitrary places. I'd recommend using the maven-antrun-plugin and ssh as in this example.

Related

IOException thrown when processing xsd dependencies in maven

I have a problem while processing .xsd file during my maven build.
I use jaxb2 plugin but I have to download external dependiencies from my .xsd files. The problem is that these dependencies (.xsd) are from enviroment which is unstable and very often my build fails because maven cannot download xsd file. How to configure jaxb plugin to force him to try download xsd few times to prevent build failure?
Part of my pom.xml configuration:
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<strict>false</strict>
<extension>true</extension>
<args>
<arg>-Xfluent-api</arg>
<arg>-XtoString</arg>
<arg>-Xsetters</arg>
<arg>-XenumValue</arg>
</args>
<plugins>
<plugin>
<groupId>net.java.dev.jaxb2-commons</groupId>
<artifactId>jaxb-fluent-api</artifactId>
<version>${jaxb.fluentapi.version}</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.3</version>
</plugin>
</plugins>
<bindingDirectory>src/main/resources/jaxb</bindingDirectory>
<bindingIncludes>
<include>bindings.xml</include>
</bindingIncludes>
<schemas>
<schema>
<fileset>
<!-- Defaults to schemaDirectory. -->
<directory>${project.basedir}/src/main/resources/orbeons</directory>
<!-- Defaults to schemaIncludes. -->
<includes>
<include>*.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/generated-sources/orbeons</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Author of the maven-jaxb2-plugin here.
You have two parts here: managing the downloads of external resources and compiling the schemas, rewriting "external" links to local files.
The first (managing downloads) is not in the scope of the maven-jaxb2-plugin, the second is supported with
catalogs.
In short, you can create a catalog file like this:
REWRITE_SYSTEM "http://www.w3.org" "w3c"
Or this:
REWRITE_SYSTEM "http://schemas.opengis.net" "maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc"
And use this file to "rewrite" absolute links to local files or resources within Maven artifacts:
<configuration>
<catalog>src/main/resources/catalog.cat</catalog>
</configuration>
As for the first part, I don't think managing downloads with retries, continuations and all other stuff should be in the scope of the JAXB2 Maven plugin.
ps. You don't need build-helper-maven-plugin/add-source with maven-jaxb2-plugin, source directory is added automatically.
Apparently the maven-jaxb2-plugin does not support such a feature. (And neither does the maven-download-plugin nor even the maven-dependency-plugin).
Three solutions come into my mind at the moment (plus two and a half inspired by LIttle Ancient Forest Kami's comment) [Numbers reflect the precedence of what I would do]:
Use a CI tool (Jenkins, etc.) that supports retry on job failure. [1]
Handmade:
Use the GMavenPlus plugin with a script ... [2]
Use the Maven AntRun plugin with a script ... [3]
Use the Exec Maven plugin with a program ... [5]
... that performs the download and retry and bind it to the generate-resources phase in your project's POM.
Create a Maven plugin with appropriate parameters (url, outputDirectory, retryCount) that uses the maven-download-plugin and performs the retry. Bind its goal to the generate-resources phase in your project's POM. [4]
Create a check-download Maven project that uses the maven-download-plugin bound to the generate-resources phase to download the .xsd. [6]
Create a shell script that contains the following (in pseudo code):
download:
counter++
<check-download project>/mvn generate-resources
if error and counter < maxRetryCount goto download
if not error
<your project>/mvn ...
else
display appropriate error message
There is also a question Maven download retry? from 2005. Unanswered.

Can I get Maven to turn a file into properties I can use in my Maven script?

I’m using Maven 3.1.0. I have a file like so
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db
username: user
password: pass
Is there a way I can get Maven to parse this file so that I can use the keys (e.g. “username”) as properties (e.g. ${username}) in other parts of my Maven script? Ultimately I want to pass these properties as connection parameters to the Maven SQL plugin.
Unfortunately, its not an option to change the format of this file.
Take a look at the Properties Maven Plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>myconfig.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The plugin should be able to handle your properties file. if not (format problems), you could include a build step to convert the file to a more convenient format.
EnvInject Plugin used to inject Variables from property files. Refer https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin

Maven Antlr3 plugin generates code in weird location

I am using plugin and Antlr version 3.3 for a project under H:/compiler
I have a tokens file under my src/main/antlr3/com/cbc/example directory called CBCTokens.g. In the same package i have a parser grammar file called MyScribe.g that references the tokens using tokenVocab=CBCTokens. I also have a tree grammar in the same directory.
However when i try to execute the build, i get an error on the very first file the plugin encounters saying:
Error(1): cannot write file : java.io.FileNotFoundException: H:\compiler\target\generated-sources\antlr3\H:\compiler\src\main\antlr3\CBCTokensLexer.java (The filname, directory name, or volume label syntax is incorrect)
It seems to me that the plugin is determining the output path using some weird combination of baseDir and the default output directory.
What configuration am i missing?
Thanks
First best thing is never to generate code into src/main/ folder whatever. This means for your configuration just remove <outputDirectory> tag and remove the <sourceDirectory> cause it's the default of the antlr3-maven-plugin.
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr3-maven-plugin</artifactId>
<version>3.3</version>
<configuration>
<printGrammar>false</printGrammar>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to include external source files in maven-jar-plugin

We are building a jar file from external(to the project) classes.
That works fine but we have not been able to figure out how to also include the external source files. I have tried using the "< includes >" tag but only end up with a manifest file in the final jar when used. I have looked at using the maven-resources-plugin but either I used it wrong or it doesn't work in my case. Here is a copy of our of code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<!-- <phase>generate-resources</phase> -->
<phase>clean</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${itendant.path}/web/rocket/WEB-INF/classes</classesDirectory>
<finalName>${itendant.jar.name}</finalName>
<outputDirectory>${itendant.jar.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Sources? Do you mean external dependencies? These should also be managed with maven, using mvn deploy:deploy-file as described on http://maven.apache.org/plugins/maven-deploy-plugin/usage.html , and imported in your pom.xml.
If you really mean external resources, then a proper resources declaration would be:
<project>
...
<build>
...
<resources>
<resource>
<directory> [your folder here] </directory>
</resource>
</resources>
...
</build>
...
</project>
You can have multiple tags if you have multiple resource directories, of course. Also note that building anything during clean is questionable, as clean is not run every build - package would be a better option.

Keep permissions on files with Maven resources:testResources

Is it possible to keep permissions on file with Maven resources:testResources? My use case is a Selenium binary driver that I put in /src/test/resources that I would like to be able to use from my tests. My -rwxr-xr-x is however changed to -rw-r--r-- in target/test-classes.
This seems to be a bug in the Maven Resource Plugin
If you are using the Maven Assembly Plugin, you can configure the file permissions there.
If not, you might consider a workaround. You could do this via Ant by doing something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>process-test-classes</id>
<phase>process-test-classes</phase>
<configuration>
<target>
<chmod file="target/test-classes/test.sh" perm="755"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I added a profile that gets activated automatically when run on a Unix machine. It executes an in-line shell script to adopt file permissions from all files in a folder recursively to files of the same name in another folder (see SRC and DST variables). The script requires a /bin/sh as well as find, xargs and chmod, which should exist on all modern systems.
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>fix-resource-permissions</id>
<goals>
<goal>exec</goal>
</goals>
<phase>process-test-resources</phase>
<configuration>
<executable>/bin/sh</executable>
<arguments>
<argument>-c</argument>
<argument>
set -x
SRC="${basedir}/src/test/resources"
DST="${project.build.directory}/test-classes"
find "$$SRC" -printf "%P\0" | xargs --verbose -0 -I {} chmod --reference="$$SRC/{}" -f "$$DST/{}"
</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
My solution is to execute the scripts in a way that do not mind the flags. So, for instance, when running from Maven Exec Plugin, I use:
Before:
<executable>myScript.sh</executable>
<commandlineArgs>...</commandlineArgs>
After:
<executable>bash</executable>
<commandlineArgs>myScript.sh ...</commandlineArgs>
Note: If you use bash -c, it will also fail if the exec flag is off.
Adding this remarks from Jason van Zyl, one of the creators of Maven:
The maven-resources-plugin was never intended to create any resources that would be used in a naked filesystem. It was strictly intended to place resources into the resultant artifact for use in a platform independent way, in general from the classpath. If you want to move archives around that are going to be unpacked and used I suggest the assembly plugin for making the archives and the dependency plugin for unpacking them.
http://maven.40175.n5.nabble.com/maven-resources-plugin-not-retaining-unix-permissions-td4938002.html

Resources