From f9a08032c5cdf6ca4dc4649992436207576fbe9e Mon Sep 17 00:00:00 2001 From: Christopher Tubbs Date: Thu, 9 Apr 2020 10:59:02 +0000 Subject: [PATCH] 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 Reviewers: Enrico Olivelli , Mate Szalay-Beko Closes #1310 from ctubbsii/use-resource-filtering-for-version-info --- pom.xml | 2 +- zookeeper-contrib/pom.xml | 13 +- zookeeper-server/pom.xml | 55 ++--- .../org/apache/zookeeper/version/Info.java | 29 +++ .../zookeeper/version/VersionInfoMain.java | 25 +++ .../apache/zookeeper/version/util/VerGen.java | 202 ------------------ .../java/org/apache/zookeeper/VerGenTest.java | 71 ------ 7 files changed, 79 insertions(+), 318 deletions(-) create mode 100644 zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/Info.java create mode 100644 zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/VersionInfoMain.java delete mode 100644 zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java delete mode 100644 zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java diff --git a/pom.xml b/pom.xml index 6fad123737f..79e88df6ca9 100755 --- a/pom.xml +++ b/pom.xml @@ -117,7 +117,7 @@ tdunning - Ted Dunning + Ted Dunning tdunning@apache.org -8 diff --git a/zookeeper-contrib/pom.xml b/zookeeper-contrib/pom.xml index 228a520ed4b..0e2959d3be0 100755 --- a/zookeeper-contrib/pom.xml +++ b/zookeeper-contrib/pom.xml @@ -49,18 +49,13 @@ + + true + + - - com.github.spotbugs - spotbugs-maven-plugin - 3.1.9 - - - true - - org.apache.maven.plugins maven-compiler-plugin diff --git a/zookeeper-server/pom.xml b/zookeeper-server/pom.xml index 21c30eae87f..117a77b8ec7 100755 --- a/zookeeper-server/pom.xml +++ b/zookeeper-server/pom.xml @@ -166,22 +166,29 @@ - + org.codehaus.mojo build-helper-maven-plugin + tbuild-time timestamp-property build.time - MM/dd/yyyy HH:mm zz + yyyy-MM-dd HH:mm zz en_US - GMT + UTC + + parse-version + + parse-version + + generate-sources @@ -197,44 +204,22 @@ org.apache.maven.plugins - maven-compiler-plugin + maven-resources-plugin - pre-compile-vergen - generate-sources - - - org/apache/zookeeper/version/**/*.java - - + prepare-filtered-java-source - compile + copy-resources - - - - - org.codehaus.mojo - exec-maven-plugin - - - generate-version-info generate-sources - - exec - - ${project.basedir}/src/main/java/ - java - - -classpath - - org.apache.zookeeper.version.util.VerGen - ${project.version} - ${mvngit.commit.id} - ${build.time} - ${project.basedir}/target/generated-sources/java - + ${project.build.directory}/generated-sources/java + + + src/main/java-filtered + true + + diff --git a/zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/Info.java b/zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/Info.java new file mode 100644 index 00000000000..5570eccf800 --- /dev/null +++ b/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}"; +} diff --git a/zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/VersionInfoMain.java b/zookeeper-server/src/main/java-filtered/org/apache/zookeeper/version/VersionInfoMain.java new file mode 100644 index 00000000000..b7d0054bd72 --- /dev/null +++ b/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}"); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java b/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java deleted file mode 100644 index 9bb699f090d..00000000000 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java +++ /dev/null @@ -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 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.
- * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier] - * rev buildDate - * - * @param args - *
    - *
  • maj - major version number - *
  • min - minor version number - *
  • micro - minor minor version number - *
  • qualifier - optional qualifier (dash followed by qualifier text) - *
  • rev - current Git revision number - *
  • buildDate - date the build - *
- */ - 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; - } - } - -} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java deleted file mode 100644 index 1542df0caa5..00000000000 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java +++ /dev/null @@ -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 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); - } - -}