浏览代码

ZOOKEEPER-3786: Simplify version generation

Simplify generation of VersionInfoMain.java and Info.java by using
maven-resource-plugin's built-in resource filtering at build time.

This eliminates the need to use VerGen to generate java source files
during the build.

Also make other slight pom improvements:
1. Remove trailing tab character in Ted's name in pom.xml
2. Simplify spotbugs skipping in contrib pom.xml
3. Add m2e configuration for build plugin executions to be ignored by
   Eclipse, for developers (like me) using Eclipse IDE
4. Format build time in a more international-friendly and less ambiguous
   way (year first, then month, then day, using UTC instead of GMT)

Link to issue: https://issues.apache.org/jira/browse/ZOOKEEPER-3786

Author: Christopher Tubbs <ctubbsii@apache.org>

Reviewers: Enrico Olivelli <eolivelli@apache.org>, Mate Szalay-Beko <symat@apache.org>

Closes #1310 from ctubbsii/use-resource-filtering-for-version-info
Christopher Tubbs 5 年之前
父节点
当前提交
f9a08032c5

+ 1 - 1
pom.xml

@@ -117,7 +117,7 @@
   <developers>
   <developers>
     <developer>
     <developer>
       <id>tdunning</id>
       <id>tdunning</id>
-      <name>Ted Dunning	</name>
+      <name>Ted Dunning</name>
       <email>tdunning@apache.org</email>
       <email>tdunning@apache.org</email>
       <timezone>-8</timezone>
       <timezone>-8</timezone>
     </developer>
     </developer>

+ 4 - 9
zookeeper-contrib/pom.xml

@@ -49,18 +49,13 @@
     </profile>
     </profile>
   </profiles>
   </profiles>
 
 
+  <properties>
+    <spotbugs.skip>true</spotbugs.skip>
+  </properties>
+
   <build>
   <build>
     <pluginManagement>
     <pluginManagement>
       <plugins>
       <plugins>
-        <plugin>
-          <groupId>com.github.spotbugs</groupId>
-          <artifactId>spotbugs-maven-plugin</artifactId>
-          <version>3.1.9</version>
-          <configuration>
-            <!-- No spotbugs for contrib modules -->
-            <skip>true</skip>
-          </configuration>
-        </plugin>
         <plugin>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <artifactId>maven-compiler-plugin</artifactId>

+ 20 - 35
zookeeper-server/pom.xml

@@ -166,22 +166,29 @@
 
 
   <build>
   <build>
     <plugins>
     <plugins>
-      <plugin> <!-- ${maven.build.timestamp} does not support timezone :( -->
+      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>build-helper-maven-plugin</artifactId>
         <artifactId>build-helper-maven-plugin</artifactId>
         <executions>
         <executions>
           <execution>
           <execution>
+            <!-- ${maven.build.timestamp} does not support timezone :( -->
             <id>tbuild-time</id>
             <id>tbuild-time</id>
             <goals>
             <goals>
               <goal>timestamp-property</goal>
               <goal>timestamp-property</goal>
             </goals>
             </goals>
             <configuration>
             <configuration>
               <name>build.time</name>
               <name>build.time</name>
-              <pattern>MM/dd/yyyy HH:mm zz</pattern>
+              <pattern>yyyy-MM-dd HH:mm zz</pattern>
               <locale>en_US</locale>
               <locale>en_US</locale>
-              <timeZone>GMT</timeZone>
+              <timeZone>UTC</timeZone>
             </configuration>
             </configuration>
           </execution>
           </execution>
+          <execution>
+            <id>parse-version</id>
+            <goals>
+              <goal>parse-version</goal>
+            </goals>
+          </execution>
           <execution>
           <execution>
             <phase>generate-sources</phase>
             <phase>generate-sources</phase>
             <goals>
             <goals>
@@ -197,44 +204,22 @@
       </plugin>
       </plugin>
       <plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
+        <artifactId>maven-resources-plugin</artifactId>
         <executions>
         <executions>
           <execution>
           <execution>
-            <id>pre-compile-vergen</id>
-            <phase>generate-sources</phase>
-            <configuration>
-              <includes>
-                <include>org/apache/zookeeper/version/**/*.java</include>
-              </includes>
-            </configuration>
+            <id>prepare-filtered-java-source</id>
             <goals>
             <goals>
-              <goal>compile</goal>
+              <goal>copy-resources</goal>
             </goals>
             </goals>
-          </execution>
-        </executions>
-      </plugin>
-      <plugin>
-        <groupId>org.codehaus.mojo</groupId>
-        <artifactId>exec-maven-plugin</artifactId>
-        <executions>
-          <execution>
-            <id>generate-version-info</id>
             <phase>generate-sources</phase>
             <phase>generate-sources</phase>
-            <goals>
-              <goal>exec</goal>
-            </goals>
             <configuration>
             <configuration>
-              <workingDirectory>${project.basedir}/src/main/java/</workingDirectory>
-              <executable>java</executable>
-              <arguments>
-                <argument>-classpath</argument>
-                <classpath />
-                <argument>org.apache.zookeeper.version.util.VerGen</argument>
-                <argument>${project.version}</argument>
-                <argument>${mvngit.commit.id}</argument>
-                <argument>${build.time}</argument>
-                <argument>${project.basedir}/target/generated-sources/java</argument>
-              </arguments>
+              <outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
+              <resources>
+                <resource>
+                  <directory>src/main/java-filtered</directory>
+                  <filtering>true</filtering>
+                </resource>
+              </resources>
             </configuration>
             </configuration>
           </execution>
           </execution>
         </executions>
         </executions>

+ 29 - 0
zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/Info.java

@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.zookeeper.version;
+
+public interface Info {
+    int MAJOR=${parsedVersion.majorVersion};
+    int MINOR=${parsedVersion.minorVersion};
+    int MICRO=${parsedVersion.incrementalVersion};
+    String QUALIFIER="${parsedVersion.qualifier}".isEmpty() ? null : "${parsedVersion.qualifier}";
+    int REVISION=-1; //@deprecated, please use REVISION_HASH
+    String REVISION_HASH="${mvngit.commit.id}";
+    String BUILD_DATE="${build.time}";
+}

+ 25 - 0
zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/VersionInfoMain.java

@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.zookeeper.version;
+
+public class VersionInfoMain implements org.apache.zookeeper.version.Info {
+    public static void main(String[] args) {
+        System.out.println("Apache ZooKeeper, version ${project.version} ${build.time}");
+    }
+}

+ 0 - 202
zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java

@@ -1,202 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.zookeeper.version.util;
-
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import org.apache.zookeeper.server.ExitCode;
-
-@SuppressFBWarnings("DM_EXIT")
-public class VerGen {
-
-    private static final String PACKAGE_NAME = "org.apache.zookeeper.version";
-    private static final String VERSION_CLASS_NAME = "VersionInfoMain";
-    private static final String VERSION_INTERFACE_NAME = "Info";
-
-    static void printUsage() {
-        System.out.print("Usage:\tjava  -cp <classpath> org.apache.zookeeper."
-                         + "version.util.VerGen maj.min.micro[-qualifier] rev buildDate outputDirectory");
-        System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-    }
-
-    public static void generateFile(File outputDir, Version version, String rev, String buildDate) {
-        String path = PACKAGE_NAME.replaceAll("\\.", "/");
-        File pkgdir = new File(outputDir, path);
-        if (!pkgdir.exists()) {
-            // create the pkg directory
-            boolean ret = pkgdir.mkdirs();
-            if (!ret) {
-                System.out.println("Cannnot create directory: " + path);
-                System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-            }
-        } else if (!pkgdir.isDirectory()) {
-            // not a directory
-            System.out.println(path + " is not a directory.");
-            System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-        }
-
-        try (FileWriter w = new FileWriter(new File(pkgdir, VERSION_INTERFACE_NAME + ".java"))) {
-            w.write("// Do not edit!\n// File generated by org.apache.zookeeper" + ".version.util.VerGen.\n");
-            w.write("/**\n");
-            w.write("* Licensed to the Apache Software Foundation (ASF) under one\n");
-            w.write("* or more contributor license agreements.  See the NOTICE file\n");
-            w.write("* distributed with this work for additional information\n");
-            w.write("* regarding copyright ownership.  The ASF licenses this file\n");
-            w.write("* to you under the Apache License, Version 2.0 (the\n");
-            w.write("* \"License\"); you may not use this file except in compliance\n");
-            w.write("* with the License.  You may obtain a copy of the License at\n");
-            w.write("*\n");
-            w.write("*     http://www.apache.org/licenses/LICENSE-2.0\n");
-            w.write("*\n");
-            w.write("* Unless required by applicable law or agreed to in writing, software\n");
-            w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n");
-            w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
-            w.write("* See the License for the specific language governing permissions and\n");
-            w.write("* limitations under the License.\n");
-            w.write("*/\n");
-            w.write("\n");
-            w.write("package " + PACKAGE_NAME + ";\n\n");
-            w.write("public interface " + VERSION_INTERFACE_NAME + " {\n");
-            w.write("    int MAJOR=" + version.maj + ";\n");
-            w.write("    int MINOR=" + version.min + ";\n");
-            w.write("    int MICRO=" + version.micro + ";\n");
-            w.write("    String QUALIFIER=" + (version.qualifier == null ? "\"\"" : "\"" + version.qualifier + "\"") + ";\n");
-            if (rev.equals("-1")) {
-                System.out.println("Unknown REVISION number, using " + rev);
-            }
-            w.write("    int REVISION=-1; //@deprecated, please use REVISION_HASH\n");
-            w.write("    String REVISION_HASH=\"" + rev + "\";\n");
-            w.write("    String BUILD_DATE=\"" + buildDate + "\";\n");
-            w.write("}\n");
-        } catch (IOException e) {
-            System.out.println("Unable to generate version.Info file: " + e.getMessage());
-            System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-        }
-
-        // Generate a main class to display version data
-        // that can be exec'd in zkServer.sh
-        try (FileWriter w = new FileWriter(new File(pkgdir, VERSION_CLASS_NAME + ".java"))) {
-            w.write("// Do not edit!\n// File generated by org.apache.zookeeper" + ".version.util.VerGen.\n");
-            w.write("/**\n");
-            w.write("* Licensed to the Apache Software Foundation (ASF) under one\n");
-            w.write("* or more contributor license agreements.  See the NOTICE file\n");
-            w.write("* distributed with this work for additional information\n");
-            w.write("* regarding copyright ownership.  The ASF licenses this file\n");
-            w.write("* to you under the Apache License, Version 2.0 (the\n");
-            w.write("* \"License\"); you may not use this file except in compliance\n");
-            w.write("* with the License.  You may obtain a copy of the License at\n");
-            w.write("*\n");
-            w.write("*     http://www.apache.org/licenses/LICENSE-2.0\n");
-            w.write("*\n");
-            w.write("* Unless required by applicable law or agreed to in writing, software\n");
-            w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n");
-            w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
-            w.write("* See the License for the specific language governing permissions and\n");
-            w.write("* limitations under the License.\n");
-            w.write("*/\n");
-            w.write("\n");
-            w.write("package " + PACKAGE_NAME + ";\n\n");
-            w.write("public class " + VERSION_CLASS_NAME + " implements " + PACKAGE_NAME + ".Info {\n");
-            w.write("    public static void main(String[] args) {\n");
-            w.write("        final String VER_STRING = MAJOR + \".\" + MINOR + \".\" + MICRO +");
-            w.write("            (QUALIFIER == null ? \"\" : \"-\" + QUALIFIER)  + \" \" +");
-            w.write("            BUILD_DATE;" + "\n");
-            w.write("        System.out.println(\"Apache ZooKeeper, version \" + VER_STRING);\n");
-            w.write("    }\n");
-            w.write("}\n");
-        } catch (IOException e) {
-            System.out.println("Unable to generate version.VersionInfoMain file: " + e.getMessage());
-            System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-        }
-    }
-
-    public static class Version {
-
-        public int maj;
-        public int min;
-        public int micro;
-        public String qualifier;
-
-    }
-
-    public static Version parseVersionString(String input) {
-        Version result = new Version();
-
-        Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)((\\.\\d+)*)(-(.+))?$");
-        Matcher m = p.matcher(input);
-
-        if (!m.matches()) {
-            return null;
-        }
-        result.maj = Integer.parseInt(m.group(1));
-        result.min = Integer.parseInt(m.group(2));
-        result.micro = Integer.parseInt(m.group(3));
-        if (m.groupCount() == 7) {
-            result.qualifier = m.group(7);
-        } else {
-            result.qualifier = null;
-        }
-        return result;
-    }
-
-    /**
-     * Emits a org.apache.zookeeper.version.Info interface file with version and
-     * revision information constants set to the values passed in as command
-     * line parameters. The file is created in the current directory. <br>
-     * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier]
-     * rev buildDate
-     *
-     * @param args
-     *            <ul>
-     *            <li>maj - major version number
-     *            <li>min - minor version number
-     *            <li>micro - minor minor version number
-     *            <li>qualifier - optional qualifier (dash followed by qualifier text)
-     *            <li>rev - current Git revision number
-     *            <li>buildDate - date the build
-     *            </ul>
-     */
-    public static void main(String[] args) {
-        if (args.length != 4) {
-            printUsage();
-        }
-        try {
-            Version version = parseVersionString(args[0]);
-            if (version == null) {
-                System.err.println("Invalid version number format, must be \"x.y.z(-.*)?\"");
-                System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
-            }
-            String rev = args[1];
-            if (rev == null || rev.trim().isEmpty()) {
-                rev = "-1";
-            } else {
-                rev = rev.trim();
-            }
-            generateFile(new File(args[3]), version, rev, args[2]);
-        } catch (NumberFormatException e) {
-            System.err.println("All version-related parameters must be valid integers!");
-            throw e;
-        }
-    }
-
-}

+ 0 - 71
zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java

@@ -1,71 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.zookeeper;
-
-import static org.junit.Assert.assertEquals;
-import java.io.File;
-import java.util.Arrays;
-import java.util.Collection;
-import org.apache.zookeeper.test.ClientBase;
-import org.apache.zookeeper.version.util.VerGen;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-/**
- * Test VerGen, used during the build.
- *
- */
-@RunWith(Parameterized.class)
-@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class)
-public class VerGenTest extends ZKTestCase {
-
-    @Parameters
-    public static Collection<Object[]> data() {
-        return Arrays.asList(new Object[][]{{"1.2.3", new Object[]{1, 2, 3, null}}, {"1.2.3-dev", new Object[]{1, 2, 3, "dev"}}, {"1.2.3-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3-foo-bar+123", new Object[]{1, 2, 3, "foo-bar+123"}}, {"1.2.3.4.5-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3.4.5-foo-bar+123", new Object[]{1, 2, 3, "foo-bar+123"}}});
-    }
-
-    private String input;
-
-    private Object[] expected;
-
-    public VerGenTest(String input, Object[] expected) {
-        this.input = input;
-        this.expected = expected;
-    }
-
-    @Test
-    public void testParser() {
-        VerGen.Version v = VerGen.parseVersionString(input);
-        assertEquals(expected[0], v.maj);
-        assertEquals(expected[1], v.min);
-        assertEquals(expected[2], v.micro);
-        assertEquals(expected[3], v.qualifier);
-    }
-
-    @Test
-    public void testGenFile() throws Exception {
-        VerGen.Version v = VerGen.parseVersionString(input);
-        File outputDir = ClientBase.createTmpDir();
-        VerGen.generateFile(outputDir, v, "1", "Nov1");
-        ClientBase.recursiveDelete(outputDir);
-    }
-
-}