diff --git a/README.md b/README.md index ac2964c..90aa20d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ The list of components currently reads as follows: * [logging-context](logging-context) Structured application state/context for logging * [logging-context-gelf](logging-context-gelf) Context-aware GELF log handler * [logging-context-json](logging-context-json) Context-aware JSON/logstash log formatter -* [logging-utilities](logging-utilities) Helpers for logging. Sensible default exception handler which logs to jul. +* [logging-utilities](logging-utilities) Helpers for logging. Sensible default exception handler which logs to jul * [pb-json](pb-json) Hassle-free conversion from Protobuf to JSON and back +* [protoc-bundled-plugin](protoc-bundled-plugin) Batteries included Protobuf compiler plugin for Maven * [emjar](emjar) Class loader and supporting cast for using jar-in-jar embedded archives as part of classpath * [emjar-maven-plugin](emjar-maven-plugin) Generate EmJar-enabled bundle archives from Maven diff --git a/amazonaws-utils/pom.xml b/amazonaws-utils/pom.xml index fe44947..704a3dc 100644 --- a/amazonaws-utils/pom.xml +++ b/amazonaws-utils/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT amazonaws-utils - Tools for AWS @@ -44,7 +44,7 @@ junit junit - 4.7 + ${junit.version} test diff --git a/emjar-maven-plugin/pom.xml b/emjar-maven-plugin/pom.xml index e032218..127b06a 100644 --- a/emjar-maven-plugin/pom.xml +++ b/emjar-maven-plugin/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT emjar-maven-plugin -- for building executable embedded jar files @@ -44,7 +44,7 @@ junit junit - 4.11 + ${junit.version} test diff --git a/emjar/pom.xml b/emjar/pom.xml index f083ff2..90f85a2 100644 --- a/emjar/pom.xml +++ b/emjar/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT emjar -- for loading dependencies from embedded jar files @@ -44,7 +44,7 @@ junit junit - 4.11 + ${junit.version} test diff --git a/logging-context-gelf/pom.xml b/logging-context-gelf/pom.xml index ad99751..b88bfb0 100644 --- a/logging-context-gelf/pom.xml +++ b/logging-context-gelf/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT logging-context-gelf - Logging with context to GELF @@ -44,7 +44,7 @@ junit junit - 4.7 + ${junit.version} test diff --git a/logging-context-json/pom.xml b/logging-context-json/pom.xml index 27e65dc..582b361 100644 --- a/logging-context-json/pom.xml +++ b/logging-context-json/pom.xml @@ -5,11 +5,11 @@ com.comoyo commons - 1.0-SNAPSHOT + 1.4-SNAPSHOT com.comoyo.commons logging-context-json - 1.0-SNAPSHOT + 1.4-SNAPSHOT logging-context-json - Logging with context to json-event logs for logstash http://github.com/comoyo/commons/ @@ -32,7 +32,7 @@ junit junit - 4.7 + ${junit.version} test @@ -50,7 +50,7 @@ com.comoyo.commons logging-context - 1.0-SNAPSHOT + 1.4-SNAPSHOT diff --git a/logging-context/pom.xml b/logging-context/pom.xml index f9e1ab7..c117af9 100644 --- a/logging-context/pom.xml +++ b/logging-context/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT logging-context - Tools for logging with context @@ -44,7 +44,7 @@ junit junit - 4.7 + ${junit.version} test diff --git a/logging-utilities/pom.xml b/logging-utilities/pom.xml index 7eb7114..678077c 100644 --- a/logging-utilities/pom.xml +++ b/logging-utilities/pom.xml @@ -5,7 +5,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT 4.0.0 diff --git a/pb-json/pom.xml b/pb-json/pom.xml index f0a0e5c..d2b1f35 100644 --- a/pb-json/pom.xml +++ b/pb-json/pom.xml @@ -8,7 +8,7 @@ commons com.comoyo - 1.0-SNAPSHOT + 1.4-SNAPSHOT pb-json - Tools for working with Protobuf and Json @@ -41,7 +41,7 @@ com.comoyo.maven.plugins protoc-bundled-plugin - ${protoc.plugin.version} + ${project.version} @@ -57,7 +57,7 @@ junit junit - 4.7 + ${junit.version} test diff --git a/pom.xml b/pom.xml index 781c7ff..3c491ec 100644 --- a/pom.xml +++ b/pom.xml @@ -8,15 +8,15 @@ UTF-8 UTF-8 - 1.3.0 1.9.13 14.0 + 4.11 Comoyo Commons Multi-Module POM com.comoyo commons - 1.0-SNAPSHOT + 1.4-SNAPSHOT Collection of utility components https://github.com/comoyo/commons @@ -70,6 +70,7 @@ pb-json + protoc-bundled-plugin amazonaws-utils logging-context logging-context-gelf diff --git a/protoc-bundled-plugin/README.md b/protoc-bundled-plugin/README.md new file mode 100644 index 0000000..36bbf1e --- /dev/null +++ b/protoc-bundled-plugin/README.md @@ -0,0 +1,51 @@ +protoc-bundled-plugin +===================== + +Maven Plugin Mojo for compiling Protobuf schema files. Protobuf compiler binaries for various platforms and protobuf versions are bundled with the plugin and used as required. + +### Available parameters + +* protobufVersion + + Protobuf version to compile schema files for. If omitted, version is inferred from the project's depended-on `com.google.com:protobuf-java` artifact, if any. The inferred version must match the explicitly given version if both are present. + +* inputDirectories + + Directories containing *.proto files to compile. Defaults to `${project.basedir}/src/main/protobuf`. + +* outputDirectory + + Output directory for generated Java class files. Defaults to `${project.build.directory}/generated-sources/protobuf`. + +* testInputDirectories + + Directories containing test *.proto files to compile. Defaults to `${project.basedir}/src/test/protobuf`. + +* testOutputDirectory + + Output directory for generated Java class test files. Defaults to `${project.build.directory}/generated-test-sources/protobuf`. + +* protocExec + + Path to existing protoc to use. Overrides auto-detection and use of bundled protoc. + +### Minimal usage example + +```xml + + ... + + com.comoyo.maven.plugins + protoc-bundled-plugin + 1.4.42 + + + + run + + + + + ... + +``` diff --git a/protoc-bundled-plugin/pom.xml b/protoc-bundled-plugin/pom.xml new file mode 100644 index 0000000..826fab9 --- /dev/null +++ b/protoc-bundled-plugin/pom.xml @@ -0,0 +1,194 @@ + + 4.0.0 + + + commons + com.comoyo + 1.4-SNAPSHOT + + + com.comoyo.maven.plugins + protoc-bundled-plugin + maven-plugin + + 3 + + + protoc-bundled-plugin -- Batteries included Maven Protobuf compiler Plugin + + https://github.com/comoyo/commons/protoc-bundled-plugin + + + 1.6 + + + + + org.apache.maven + maven-core + 3.0.4 + + + + org.apache.maven + maven-plugin-api + 3.0.5 + + + + org.apache.maven + maven-project + 2.0 + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + provided + + + + commons-io + commons-io + 2.4 + + + + org.codehaus.plexus + plexus-utils + 3.0.8 + + + + junit + junit + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.2 + + protoc-bundled + true + + + + mojo-descriptor + + descriptor + + + + help-goal + + helpmojo + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + + + attach-artifacts + package + + attach-artifact + + + + + + src/main/binaries/2.3.0/protoc-2.3.0-linux-x86 + exe + 2.3.0-linux-x86 + + + src/main/binaries/2.3.0/protoc-2.3.0-linux-x86_64 + exe + 2.3.0-linux-x86_64 + + + src/main/binaries/2.3.0/protoc-2.3.0-mac_os_x-x86_64 + exe + 2.3.0-mac_os_x-x86_64 + + + src/main/binaries/2.3.0/protoc-2.3.0-win32-x86.exe + exe + 2.3.0-win32-x86 + + + + + src/main/binaries/2.4.1/protoc-2.4.1-linux-x86 + exe + 2.4.1-linux-x86 + + + src/main/binaries/2.4.1/protoc-2.4.1-linux-x86_64 + exe + 2.4.1-linux-x86_64 + + + src/main/binaries/2.4.1/protoc-2.4.1-mac_os_x-x86_64 + exe + 2.4.1-mac_os_x-x86_64 + + + src/main/binaries/2.4.1/protoc-2.4.1-win32-x86.exe + exe + 2.4.1-win32-x86 + + + + + src/main/binaries/2.5.0/protoc-2.5.0-linux-x86 + exe + 2.5.0-linux-x86 + + + src/main/binaries/2.5.0/protoc-2.5.0-linux-x86_64 + exe + 2.5.0-linux-x86_64 + + + src/main/binaries/2.5.0/protoc-2.5.0-mac_os_x-x86_64 + exe + 2.5.0-mac_os_x-x86_64 + + + src/main/binaries/2.5.0/protoc-2.5.0-win32-x86.exe + exe + 2.5.0-win32-x86 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${project.build.sourceEncoding} + ${java.version} + ${java.version} + + + + + diff --git a/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86 b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86 new file mode 100755 index 0000000..6dbcc8b Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86_64 new file mode 100755 index 0000000..835ca41 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-linux-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-mac_os_x-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-mac_os_x-x86_64 new file mode 100755 index 0000000..56f87b3 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-mac_os_x-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-win32-x86.exe b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-win32-x86.exe new file mode 100755 index 0000000..2c2877b Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.3.0/protoc-2.3.0-win32-x86.exe differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86 b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86 new file mode 100755 index 0000000..6fa5716 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86_64 new file mode 100755 index 0000000..1c7b540 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-linux-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-mac_os_x-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-mac_os_x-x86_64 new file mode 100755 index 0000000..0afeef1 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-mac_os_x-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-win32-x86.exe b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-win32-x86.exe new file mode 100755 index 0000000..2d8d4a5 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.4.1/protoc-2.4.1-win32-x86.exe differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86 b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86 new file mode 100755 index 0000000..d27ae7b Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86_64 new file mode 100755 index 0000000..b95b69f Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-linux-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-mac_os_x-x86_64 b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-mac_os_x-x86_64 new file mode 100755 index 0000000..9ee3362 Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-mac_os_x-x86_64 differ diff --git a/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-win32-x86.exe b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-win32-x86.exe new file mode 100755 index 0000000..caeaf6c Binary files /dev/null and b/protoc-bundled-plugin/src/main/binaries/2.5.0/protoc-2.5.0-win32-x86.exe differ diff --git a/protoc-bundled-plugin/src/main/binaries/COPYING-protoc.txt b/protoc-bundled-plugin/src/main/binaries/COPYING-protoc.txt new file mode 100644 index 0000000..705db57 --- /dev/null +++ b/protoc-bundled-plugin/src/main/binaries/COPYING-protoc.txt @@ -0,0 +1,33 @@ +Copyright 2008, Google Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Code generated by the Protocol Buffer compiler is owned by the owner +of the input file used when generating it. This code is not +standalone and requires a support library to be linked with it. This +support library is itself covered by the above license. diff --git a/protoc-bundled-plugin/src/main/java/com/comoyo/maven/plugins/protoc/ProtocBundledMojo.java b/protoc-bundled-plugin/src/main/java/com/comoyo/maven/plugins/protoc/ProtocBundledMojo.java new file mode 100644 index 0000000..a0ff5a8 --- /dev/null +++ b/protoc-bundled-plugin/src/main/java/com/comoyo/maven/plugins/protoc/ProtocBundledMojo.java @@ -0,0 +1,429 @@ +/** + * Copyright (C) 2013-2014 Telenor Digital AS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.comoyo.maven.plugins.protoc; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.RepositorySystem; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.IOFileFilter; +import org.apache.commons.io.filefilter.SuffixFileFilter; +import org.apache.commons.io.filefilter.TrueFileFilter; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.List; + + +/** + * Maven Plugin Mojo for compiling Protobuf schema files. Protobuf + * compiler binaries for various platforms and protobuf versions are + * bundled with the plugin and used as required. + *

+ * The included ``protoc'' binary is: + *

+ * Copyright 2008, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Code generated by the Protocol Buffer compiler is owned by the owner
+ * of the input file used when generating it.  This code is not
+ * standalone and requires a support library to be linked with it.  This
+ * support library is itself covered by the above license.
+ * 
+ * + * @goal run + * @phase generate-sources + * @requiresDependencyResolution + */ + +public class ProtocBundledMojo extends AbstractMojo +{ + /** + * Plugin descriptor. + * + * @component role="org.apache.maven.plugin.descriptor.PluginDescriptor" + * @required + * @readonly + */ + private PluginDescriptor pluginDescriptor; + + /** + * The Maven project. + * + * @parameter property="project" + * @required + * @readonly + */ + private MavenProject project; + + /** + * Used to look up Artifacts in the remote repository. + * + * @component role="org.apache.maven.repository.RepositorySystem" + * @required + * @readonly + */ + protected RepositorySystem repositorySystem; + + /** + * List of Remote Repositories used by the resolver + * + * @parameter property="project.remoteArtifactRepositories" + * @readonly + * @required + */ + protected List remoteRepositories; + + + /** + * Protobuf version to compile schema files for. If omitted, + * version is inferred from the project's depended-on + * com.google.com:protobuf-java artifact, if any. (If both are + * present, the version must match.) + * + * @parameter property="protobufVersion" + */ + private String protobufVersion; + + /** + * Directories containing *.proto files to compile. + * + * @parameter + * property="inputDirectories" + */ + private File[] inputDirectories; + + /** + * Output directory for generated Java class files. + * + * @parameter + * property="outputDirectory" + * default-value="${project.build.directory}/generated-sources/protobuf" + */ + private File outputDirectory; + + /** + * Directories containing *.proto files to compile for test. + * + * @parameter + * property="testInputDirectories" + */ + private File[] testInputDirectories; + + /** + * Output directory for generated Java class files. + * + * @parameter + * property="testOutputDirectory" + * default-value="${project.build.directory}/generated-test-sources/protobuf" + */ + private File testOutputDirectory; + + /** + * Path to existing protoc to use. Overrides auto-detection and + * use of bundled protoc. + * + * @parameter property="protocExec" + */ + private File protocExec; + + + private static final Map osNamePrefix = new HashMap(); + private static final Map osArchCanon = new HashMap(); + static { + osNamePrefix.put("linux", "linux"); + osNamePrefix.put("mac os x", "mac_os_x"); + osNamePrefix.put("windows", "win32"); + + osArchCanon.put("x86_64", "x86_64"); + osArchCanon.put("amd64", "x86_64"); + osArchCanon.put("x86", "x86"); + osArchCanon.put("i386", "x86"); + osArchCanon.put("i686", "x86"); + } + + /** + * Dynamically determine name of protoc variant suitable for + * current system. + * + */ + private String determineProtocForSystem() + throws MojoExecutionException + { + final String osName = System.getProperty("os.name"); + if (osName == null) { + throw new MojoExecutionException("Unable to determine OS platform"); + } + + String os = null; + for (String prefix : osNamePrefix.keySet()) { + if (osName.toLowerCase().startsWith(prefix)) { + os = osNamePrefix.get(prefix); + break; + } + } + if (os == null) { + throw new MojoExecutionException("Unable to determine OS class for " + osName); + } + + final String archName = System.getProperty("os.arch"); + if (archName == null) { + throw new MojoExecutionException("Unable to determine CPU architecture"); + } + + String arch = osArchCanon.get(archName.toLowerCase()); + if (arch == null) { + throw new MojoExecutionException("Unable to determine CPU arch id for " + archName); + } + + // Pre-built protoc binaries for Windows use the same image + // for 32- and 64-bit systems. + if ("win32".equals(os) && "x86_64".equals(arch)) { + arch = "x86"; + } + + return protobufVersion + "-" + os + "-" + arch; + } + + /** + * Return reference to suitable protoc binary artifact, download + * from remote repository if necessary. + * + * @param protocName protoc specifier + */ + private File resolveProtocArtifact(String protocName) + throws MojoExecutionException + { + Artifact artifact + = repositorySystem.createArtifactWithClassifier( + pluginDescriptor.getGroupId(), + pluginDescriptor.getArtifactId(), + pluginDescriptor.getVersion(), + "exe", protocName); + + getLog().info("Using protoc " + artifact); + ArtifactResolutionRequest request = new ArtifactResolutionRequest() + .setArtifact(artifact) + .setRemoteRepositories(remoteRepositories); + ArtifactResolutionResult result = repositorySystem.resolve(request); + if (!result.isSuccess()) { + throw new MojoExecutionException( + "Unable to resolve dependency on protoc binary artifact, sorry: " + + result.toString()); + } + + Set artifacts = result.getArtifacts(); + if (artifacts.size() != 1) { + throw new MojoExecutionException( + "Unexpected number of artifacts returned when resolving protoc binary (" + artifacts.size() + ")"); + } + Artifact protocArtifact = artifacts.iterator().next(); + File file = protocArtifact.getFile(); + file.setExecutable(true, false); + return file; + } + + /** + * Get version of specified artifact from the current project's + * dependencies, if it exists. + * + * @param groupId + * @param artifactId + */ + private String getArtifactVersion(String groupId, String artifactId) + throws MojoExecutionException + { + Set artifacts = project.getArtifacts(); + for (Artifact artifact : artifacts) { + if (groupId.equals(artifact.getGroupId()) + && artifactId.equals(artifact.getArtifactId())) + { + return artifact.getVersion(); + } + } + return null; + } + + /** + * Ensure we have a suitable protoc binary available. If + * protocExec is explicitly given, use that. Otherwise find and + * extract suitable protoc from plugin bundle. + * + */ + private void ensureProtocBinaryPresent() + throws MojoExecutionException + { + if (protocExec != null) { + return; + } + + final String protobufArtifactVersion + = getArtifactVersion("com.google.protobuf", "protobuf-java"); + + if (protobufVersion == null) { + if (protobufArtifactVersion == null) { + throw new MojoExecutionException( + "protobufVersion not specified and unable to derive version " + + "from protobuf-java dependency"); + } + protobufVersion = protobufArtifactVersion; + } + else { + if (protobufArtifactVersion != null + && !protobufVersion.equals(protobufArtifactVersion)) + { + getLog().warn("Project includes protobuf-java artifact of version " + + protobufArtifactVersion + + " while protoc is set to compile for version " + + protobufVersion); + } + } + + final String protocName = determineProtocForSystem(); + protocExec = resolveProtocArtifact(protocName); + } + + /** + * Compile single protobuf schema file. + * + * @param dir base dir for input file, used to resolve includes + * @param input input file to compile + */ + private void compileFile(File inputDir, File input, File outputDir) + throws MojoExecutionException + { + try { + final Process proc + = new ProcessBuilder(protocExec.toString(), + "--proto_path=" + inputDir.getAbsolutePath(), + "--java_out=" + outputDir, + input.getAbsolutePath()) + .redirectErrorStream(true) + .start(); + final BufferedReader procOut + = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while (true) { + final String line = procOut.readLine(); + if (line == null) { + break; + } + getLog().info("[protoc] " + line); + } + final int status = proc.waitFor(); + procOut.close(); + if (status != 0) { + throw new MojoExecutionException( + "Compilation failure signalled by protoc exit status: " + status); + } + } + catch (Exception e) { + throw new MojoExecutionException("Unable to compile " + input.toString(), e); + } + } + + /** + * Compile all *.proto files found under inputDirectories. + * + */ + private boolean compileAllFiles(String tag, File[] inputDirs, File outputDir) + throws MojoExecutionException + { + final IOFileFilter filter = new SuffixFileFilter(".proto"); + + boolean seen = false; + for (File inputDir : inputDirs) { + if (!inputDir.exists()) { + continue; + } + if (!outputDir.exists()) { + outputDir.mkdirs(); + } + getLog().info("Compiling " + inputDir + " to " + outputDir + " [" + tag + "]"); + Iterator files + = FileUtils.iterateFiles(inputDir, filter, TrueFileFilter.INSTANCE); + while (files.hasNext()) { + final File input = files.next(); + compileFile(inputDir, input, outputDir); + seen = true; + } + } + return seen; + } + + /** + * Plugin invocation point. + * + */ + @Override + public void execute() + throws MojoExecutionException + { + ensureProtocBinaryPresent(); + + if (inputDirectories.length == 0) { + inputDirectories = new File[]{new File(project.getBasedir(), "src/main/protobuf")}; + } + if (compileAllFiles("main", inputDirectories, outputDirectory)) { + project.addCompileSourceRoot(outputDirectory.getAbsolutePath()); + } + + if (testInputDirectories.length == 0) { + testInputDirectories = new File[]{new File(project.getBasedir(), "src/test/protobuf")}; + } + if (compileAllFiles("test", testInputDirectories, testOutputDirectory)) { + project.addTestCompileSourceRoot(testOutputDirectory.getAbsolutePath()); + } + } +} diff --git a/protoc-bundled-plugin/src/main/scripts/build-protoc.sh b/protoc-bundled-plugin/src/main/scripts/build-protoc.sh new file mode 100755 index 0000000..f66a661 --- /dev/null +++ b/protoc-bundled-plugin/src/main/scripts/build-protoc.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Note: Cross compiling to 32 bit on ubuntu x86_64 requires g++-multilib and libz-dev:i386 + +ver=${1:-2.4.1} +host=$(uname -s -m | tr 'A-Z ' 'a-z-' | sed 's/darwin/mac_os_x/') + +build() { + local sys=$1; shift # Remaining args passed directly to build + + local build=target/build-$sys + local inst=$PWD/target/inst-$sys/$ver + local makeopts="LDFLAGS=-all-static" # Very static + # local makeopts="LDFLAGS=-static-libgcc CXXFLAGS=-static-libstdc++" # Quite static + + echo Building protoc $ver for $sys + wget -nc http://protobuf.googlecode.com/files/protobuf-$ver.tar.bz2 + rm -rf "$build" "$inst" + mkdir -p "$build" + tar -C "$build" -jxf protobuf-$ver.tar.bz2 + (cd $build/protobuf-$ver && \ + env "$@" ./configure --disable-shared --prefix=$inst && \ + make $makeopts -j4 install) || exit + + echo + echo '----------------------------------------------------------------------' + + local instdir=src/main/binaries/$ver + mkdir -p $instdir + local final=$instdir/protoc-$ver-$sys + cp -a $inst/bin/protoc $final + strip $final + ls -l $final + file $final + $final --version +} + +build $host +if [ "$host" = "linux-x86_64" ]; then + build "linux-x86" CXXFLAGS=-m32 CFLAGS=-m32 +fi