diff --git a/java/build.xml b/java/build.xml index 0bb36aa78..63398b58f 100644 --- a/java/build.xml +++ b/java/build.xml @@ -21,7 +21,54 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -53,7 +100,8 @@ - + diff --git a/java/pom.xml b/java/pom.xml index a3706d5ea..04015a3cc 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -94,6 +94,87 @@ + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + + build-metadata + generate-sources + + exec + + + java + + -jar + ../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar + BuildMetadataProtoFromXml + ../resources/PhoneNumberMetaData.xml + src + false + false + + + + + build-test-metadata + generate-test-sources + + exec + + + java + + -jar + ../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar + BuildMetadataProtoFromXml + ../resources/PhoneNumberMetaDataForTesting.xml + test + true + false + + + + + build-geo-data + generate-sources + + exec + + + java + + -jar + ../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar + GenerateAreaCodeData + ../resources/geocoding + src/com/google/i18n/phonenumbers/geocoding/data + false + + + + + build-geo-test-data + generate-test-sources + + exec + + + java + + -jar + ../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar + GenerateAreaCodeData + ../resources/test/geocoding + test/com/google/i18n/phonenumbers/geocoding/testing_data + true + + + + + org.apache.maven.plugins maven-jar-plugin diff --git a/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java b/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java index e67217c11..07fd3e9f9 100644 --- a/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java +++ b/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * Copyright (C) 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,9 +12,10 @@ * 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. - * - * This file is automatically generated by BuildMetadataProtoFromXml. Please - * don't modify directly. + */ + +/* This file is automatically generated by {@link BuildMetadataProtoFromXml}. + * Please don't modify it directly. */ package com.google.i18n.phonenumbers; diff --git a/java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java b/java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java index 45ef033ea..45d352b10 100644 --- a/java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java +++ b/java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * Copyright (C) 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,9 +12,10 @@ * 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. - * - * This file is automatically generated by BuildMetadataProtoFromXml. Please - * don't modify directly. + */ + +/* This file is automatically generated by {@link BuildMetadataProtoFromXml}. + * Please don't modify it directly. */ package com.google.i18n.phonenumbers; diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java index 7efd7d19e..a89f1ff9b 100644 --- a/tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java +++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java @@ -27,18 +27,18 @@ public class CopyrightNotice { public static final String TEXT = "/*\n" + - " * Copyright (C) " + Calendar.getInstance().get(Calendar.YEAR) + " Google Inc.\n" + + " * Copyright (C) " + Calendar.getInstance().get(Calendar.YEAR) + " Google Inc.\n" + " *\n" + - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + - " * you may not use this file except in compliance with the License.\n" + - " * You may obtain a copy of the License at\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + " *\n" + - " * http://www.apache.org/licenses/LICENSE-2.0\n" + + " * http://www.apache.org/licenses/LICENSE-2.0\n" + " *\n" + - " * Unless required by applicable law or agreed to in writing, software\n" + - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + - " * See the License for the specific language governing permissions and\n" + - " * limitations under the License.\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + " */\n"; } diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java index 97059bcc3..425ddd1c9 100644 --- a/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java +++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java @@ -18,10 +18,13 @@ package com.google.i18n.phonenumbers.tools; import java.io.Closeable; +import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** - * Helper class containing methods designed to ease file manipulation. + * Helper class containing methods designed to ease file manipulation and generation. * * @author Philippe Liard */ @@ -49,4 +52,36 @@ public class FileUtils { close(closeable); } } + + /** + * Returns true if the provided output file/directory is older than the provided input + * file/directory. The last modification time of a directory is the maximum last + * modification time of its children. It assumes the provided output directory only contains + * generated files. + */ + public static boolean isGenerationRequired(File inputFile, File outputDir) { + if (!outputDir.exists()) { + return true; + } + return getLastModificationTime(inputFile) > getLastModificationTime(outputDir); + } + + /** + * Gets the modification time of the most recently modified file contained in a directory. + */ + private static long getLastModificationTime(File file) { + if (!file.isDirectory()) { + return file.lastModified(); + } + long maxModificationTime = 0; + + for (File child : file.listFiles()) { + long modificationTime = getLastModificationTime(child); + + if (modificationTime > maxModificationTime) { + maxModificationTime = modificationTime; + } + } + return maxModificationTime; + } } diff --git a/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar b/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar index 49995f67f..2cf118cc3 100644 Binary files a/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar and b/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar differ diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java index a37fd3bd6..c1e72eb67 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java @@ -300,7 +300,7 @@ public class BuildMetadataJsonFromXml extends Command { jsArrayBuilder.append(null); } // optional bool same_mobile_and_fixed_line_pattern = 18 [default=false]; - if (metadata.getSameMobileAndFixedLinePattern()) { + if (metadata.isSameMobileAndFixedLinePattern()) { jsArrayBuilder.append(1); } else { jsArrayBuilder.append(null); diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java index 2e488c1c6..c5bfc7b46 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java @@ -20,6 +20,7 @@ import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; import java.io.BufferedWriter; +import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; @@ -38,7 +39,7 @@ public class BuildMetadataProtoFromXml extends Command { private static final String META_DATA_FILE_PREFIX = "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto"; private static final String TEST_META_DATA_FILE_PREFIX = - "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting"; + "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting"; private static final String TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME = "CountryCodeToRegionCodeMapForTesting"; private static final String COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME = @@ -50,7 +51,7 @@ public class BuildMetadataProtoFromXml extends Command { "\n" + "where:\n" + " inputFile The input file containing phone number metadata in XML format.\n" + - " outputDir The output directory to store phone number metadata in proto\n" + + " outputDir The output source directory to store phone number metadata in proto\n" + " format (one file per region) and the country code to region code\n" + " mapping file.\n" + " forTesting Flag whether to generate metadata for testing purposes or not.\n" + @@ -67,6 +68,11 @@ public class BuildMetadataProtoFromXml extends Command { "Example command line invocation:\n" + "BuildMetadataProtoFromXml PhoneNumberMetadata.xml src false false\n"; + private static final String GENERATION_COMMENT = + "/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.\n" + + " * Please don't modify it directly.\n" + + " */\n"; + @Override public String getCommandName() { return "BuildMetadataProtoFromXml"; @@ -84,6 +90,12 @@ public class BuildMetadataProtoFromXml extends Command { boolean forTesting = args[3].equals("true"); boolean liteBuild = args.length > 4 && args[4].equals("true"); + if (!FileUtils.isGenerationRequired( + new File(inputFile), + new File(outputDir, new File(META_DATA_FILE_PREFIX).getParent()))) { + System.out.println("Metadata code up-to-date."); + return true; + } String filePrefix; if (forTesting) { filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX; @@ -113,6 +125,7 @@ public class BuildMetadataProtoFromXml extends Command { System.err.println(HELP_MESSAGE); return false; } + System.out.println("Metadata code successfully generated."); return true; } @@ -140,15 +153,14 @@ public class BuildMetadataProtoFromXml extends Command { } else { mappingClassName = COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME; } - String mappingFile = - outputDir + "/" + PACKAGE_NAME.replaceAll("\\.", "/") + "/" + mappingClassName + ".java"; + String mappingFile = outputDir + "/" + PACKAGE_NAME + "/" + mappingClassName + ".java"; int capacity = (int) (countryCodeToRegionCodeMap.size() / MAPPING_LOAD_FACTOR); BufferedWriter writer = new BufferedWriter(new FileWriter(mappingFile)); - writer.write(CopyrightNotice.TEXT); + writer.write(CopyrightNotice.TEXT + "\n" + GENERATION_COMMENT + "\n"); if (PACKAGE_NAME.length() > 0) { - writer.write("package " + PACKAGE_NAME + ";\n\n"); + writer.write("package " + PACKAGE_NAME.replaceAll("/", ".") + ";\n\n"); } writer.write(MAPPING_IMPORTS); writer.write("\n"); diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java index a21e212ec..7951f9188 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java @@ -236,6 +236,10 @@ public class GenerateAreaCodeData extends Command { * @throws FileNotFoundException */ public void run() throws FileNotFoundException, IOException { + if (!FileUtils.isGenerationRequired(inputPath, outputPath)) { + LOGGER.log(Level.INFO, "Geocoding data up-to-date."); + return; + } List> inputOutputMappings = createInputOutputFileMappings(); SortedMap> availableDataFiles = new TreeMap>(); @@ -268,6 +272,7 @@ public class GenerateAreaCodeData extends Command { } finally { closeFile(fileOutputStream); } + LOGGER.log(Level.INFO, "Geocoding data successfully generated."); } @Override diff --git a/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar b/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar new file mode 100644 index 000000000..09500efe2 Binary files /dev/null and b/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar differ