Integrate Protocol Buffers into Maven2 build - maven-2

I'm experimenting with Protocol Buffers in an existing, fairly vanilla Maven 2 project. Currently, I invoke a shell script every time I need to update my generated sources. This is obviously a hassle, as I would like the sources to be generated automatically before each build. Hopefully without resorting to shameful hackery.
So, my question is two-fold:
Long shot: is there a "Protocol Buffers plugin" for Maven 2 that can achieve the above in an automagic way? There's a branch on Google Code whose author appears to have taken a shot at implementing such a plugin. Unfortunately, it hasn't passed code review or been merged into protobuf trunk. The status of that plugin is thus unknown.
Probably more realistic: lacking an actual plugin, how else might I go about invoking protoc from my Maven 2 build? I suppose I may be able to wire up my existing shell script into an antrun invocation or something similar.
Personal experiences are most appreciated.

You'll find some information about the plugin available in the Protocol Buffers repository in the Protocol Buffers Compiler Maven Plug-In thread on the Protocol Buffers discussion group. My understanding is that it's usable but lacking tests. I'd give it a try.
Or you could just use the antrun plugin (snipet pasted from the thread mentioned above):
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="target/generated-sources"/>
<exec executable="protoc">
<arg value="--java_out=target/generated-sources"/>
<arg value="src/main/protobuf/test.proto"/>
</exec>
</tasks>
<sourceRoot>target/generated-sources</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>

The accepted answer encouraged me to get the Google-provided plugin to work. I merged the branch mentioned in my question into a checkout of 2.2.0 source code, built and installed/deployed the plugin, and was able to use it in my project as follows:
<build>
<plugins>
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<id>generate-sources</id>
<goals>
<goal>compile</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<protoSourceRoot>${basedir}/src/main/protobuf/</protoSourceRoot>
<includes>
<param>**/*.proto</param>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
</plugin>
</plugins>
</build>
Note that I changed the plugin's version to 0.0.1 (no -SNAPSHOT) in order to make it go into my non-snapshot thirdparty Nexus repository. YMMV. The takeaway is that this plugin will be usable once it's no longer necessary to jump through hoops in order to get it going.

The accepted solution does not scale for multiple proto files. I had to come up with my own:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile-protoc</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${generated.sourceDirectory}" />
<path id="proto.path">
<fileset dir="src/main/proto">
<include name="**/*.proto" />
</fileset>
</path>
<pathconvert pathsep=" " property="proto.files" refid="proto.path" />
<exec executable="protoc" failonerror="true">
<arg value="--java_out=${generated.sourceDirectory}" />
<arg value="-I${project.basedir}/src/main/proto" />
<arg line="${proto.files}" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</build>

There's also great plugin by Igor Petruk named protobuf-maven-plugin. It's in central repo now and plays nicely with eclipse (m2e-1.1 is recommended).

I just updated the maven plugin to work with 2.2.0 -- the updated pom are attached to the code review bug.
Here are the instructions to build the plugin yourself:
svn co http://protobuf.googlecode.com/svn/branches/maven-plugin/tools/maven-plugin
cd maven-plugin
wget -O pom.xml 'http://protobuf.googlecode.com/issues/attachment?aid=8860476605163151855&name=pom.xml'
mvn install
You can then use the maven config above.

I just tried a less official but very recent (v 0.1.7) fork from https://github.com/dtrott/maven-protoc-plugin and it worked very well, courtesy of David Trott. I tested it with a couple of Maven modules one of which contained DTO-style messages and the other a service depending on them. I borrowed the plugin configuration MaxA posted on Oct 16 '09, I had protoc on my PATH and I added
<temporaryProtoFileDirectory>${basedir}/target/temp</temporaryProtoFileDirectory>
right after
<protocExecutable>protoc</protocExecutable>.
What is really nice is that all I had to do is to declare a normal dependency from the service module on the DTO module. The plugin was able to resolve proto files dependencies by finding the proto files packaged with the DTO module, extracting them to a temporary directory and using while generating code for the service. And it was smart enough not to package a second copy of the generated DTO classes with the service module.

There is a maven plugin for protobuf. https://www.xolstice.org/protobuf-maven-plugin/usage.html
The minimal config
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

I think that using antrun to invoke non-Maven steps is the generally accepted solution.
You could also try the maven-exec-plugin.

I forked of the plugin from David Trott and have it compiling multiple languages which makes it a lot more useful. See the github project here and a tutorial on integrating it with a maven build here.

Related

How to stop Apache maven <outputDirectory> to overwrite the folder

I have the following directory hierarchy:
generated
|
| -->java
Java directory has the following package: com.model
that contains java models that I copy/paste from somewhere before I compile the application.
The issue that I use Protocol buffer and I tell maven to output the generated files on same previous directory BUT over a new package:
Result : Protocol buffer generates the new package and deletes the old package.
I have no idea why does it do that although the package names are different?
Here is that part of pom I use to generate java from protocol buffer:
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<configuration>
<protocExecutable>C:\protoc.exe</protocExecutable>
<protoSourceRoot>./src/proto</protoSourceRoot>
<outputDirectory>./src/generated/java</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
if you look at the code for the plugin you'll see that the code has been hardcoded to clean the directory:
https://github.com/dtrott/maven-protoc-plugin/blob/master/src/main/java/com/google/protobuf/maven/AbstractProtocMojo.java#L154
// Quick fix to fix issues with two mvn installs in a row (ie no clean)
cleanDirectory(outputDirectory);
There's 2 ways to solve this..either set the output directory to a temp directory and then use the maven copy plugin or the maven build plugin to copy the files into the directory of your choice, or modify the maven plugin to remove that line (or better yet make it configurable).
Tommy
I have solved my issue by the following :
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete dir="./destination"/>
<copy todir="./destination">
<fileset dir="./source"/>
</copy>
<delete dir="./source"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
However , I get this error "An Ant BuildException has occured: Only one of tofile and todir may be set"

exec-maven-plugin want to pass maven.dependency.classpath as a variable in my EXEC Goal

I want to define property or want to use maven.plugin.classpath and maven.dependency.classpath in my build.xml.
How can i do it ?
Sample code is as below...
<property> </property> is not working and not able to read the values from my build.xml so please explain me how can i do it ?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<property name="maven_dependency_classpath" refid="maven.dependency.classpath" />
<executable>antscript.bat</executable> <!-- ant -f build.xml build -->
</configuration>
</plugin>
Hi Sean Patrick Floyd,
Yes i tried using maven-antrun-plugin but i am not able to setup JDK 1.4.2 version in it. I am trying to specify all possible way to apply JDK version 1.4.2 but it's still taking tools.jar or JDK version, Which maven.bat file is using (jdk 1.5)
I was using following code in MAVEN-ANTRUN-PLUGIN as below code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerVersion>${java-version}</compilerVersion>
<executable>${java.1.4.2.home}/bin/javac</executable>
<target>
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<property name="maven_dependency_classpath" refid="maven.dependency.classpath" />
<ant antfile="ant_build.xml" />
</target>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.1.4.2.home}/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>rt</artifactId>
<version>${java-version}</version>
<scope>system</scope>
<systemPath>${java.1.4.2.home}/jre/lib/rt.jar</systemPath>
</dependency>
</dependencies>
</plugin>
That's why i choose exec GOAL where my SYSTEM JAVA_HOME is 1.4.2 and it's able to execute it if i have all dependencies which i needed.
Please help me out.
Thanks.
maven.plugin.classpath etc. are variables defined in the maven-antrun-plugin only. The exec plugin doesn't know about these values. Also, it wouldn't be possible to do it like that in the first place, as you are calling an external .bat file and hence starting a new process.
If I were you I'd use the antrun plugin. See the usage page for details.
Update: ok, now I see your problem. No, you can't use a different JDK, since antrun works within the same vm. So either you need to switch the JDK maven uses, or you do in fact need to use the exec-maven-plugin. In the latter case, you will have to use
dependency:build-classpath -DoutputFile=someFile.txt
and on the ant side, read the contents of someFile.txt as a property and create a classpath from it. Or you can use the %classpath variable placeholder in your command line args.
Yes correct answer which i found is to Use MAVEN version which supports JDK 1.4.2. So i have to use apache-maven-2.0.11 which supports JDK 1.4.2
Thanks all for your answers.

Maven - Replace a file in a Jar

I want to replace a file in an existing jar/zip file while doing a Maven build. What is the easiest way to achieve this?
My favorite for this sort of tasks is maven-antrun-plugin which brings you complete ant functionality.
You can use it like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>repack</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- note that here we reference previously declared dependency -->
<unzip src="${org.apache:common-util:jar}" dest="${project.build.directory}/tmp"/>
<!-- now do what you need to any of unpacked files under target/tmp/ -->
<zip basedir="${project.build.directory}/tmp" destfile="${project.build.directory}/common-util-modified.jar"/>
<!-- now the modified jar is available -->
</target>
</configuration>
</execution>
</executions>
</plugin>
But remember - never modify any files in your local repository - in this example pointed to by ${org.apache:common-util:jar}. Doing so would affect your further builds of all your projects on the same computer (= against the same local repo).
Such builds are also irreproducible (or hard to reproduce) on other machines.
I don't think there is a dedicated plugin to do this but I would imagine you can use the exec plugin and information from Updating .class file in jar to accomplish this.

Copy dependencies maven war

When I try to use the plugin "maven-war-plugin", it copies all libraries to / WEB-INF/lib, how to copy to another directory? Example: "/ libtest"
I'm not sure to understand why you need to do this but I see two points here:
Avoiding dependencies to get copied into WEB-INF/lib (if not, then just skip the part related to 1.)
Getting them copied to another directory.
For 1. I'm assuming you need the dependencies (because you want to compile against them) but if you don't want the Maven War Plugin to copy them in WEB-INF/lib, you'll have to play with their scope, for example by declaring them as provided.
For 2. the Maven Dependency Plugin will be helpful here and I think you could use dependency:copy-dependencies, for example during the pre-package phase. Use it like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>pre-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
And configure the outputDirectory (and other parameters you could need).
Use the maven-dependency-plugin.

Is there a SVN Maven?

Is there something for maven like http://subclipse.tigris.org/svnant.html ? Something to automate your project updates without having to click update?
The Maven SCM Plugin does support Subversion. Check the Usage page.
There is always the possibility to run ant inside maven.
Ant is much more powerful than maven in many respects, but maven excels in dependency management and ease of deployment. So if you need the power of ant inside maven, use the maven antrun plugin:
Taken from
http://maven.apache.org/plugins/maven-antrun-plugin/usage.html :
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase> <!-- a lifecycle phase --> </phase>
<configuration>
<tasks>
<!--
Place any Ant task here. You can add anything
you can add between <target> and </target> in a
build.xml.
-->
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Sean