Java tutorial
/** * Copyright (C) 2015 Arne Binder (arne.b.binder@gmail.com) * * 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 licenseUtil; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.eclipse.jgit.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.HashMap; /** * Created by Arne Binder (arne.b.binder@gmail.com) on 05.08.2015. */ public class LicenseUtil { static final Logger logger = LoggerFactory.getLogger(LicenseUtil.class); static final String LICENSE_3RD_PARTY_FN = "LICENSE-3RD-PARTY"; static final String EFFECTIVE_POM_FN = "effective-pom.xml"; static final String POM_FN = "pom.xml"; static final String README_PATH = "README.md"; public static void main(String[] args) throws IOException, IncompleteLicenseObjectException { if (args.length == 0) { logger.error("Missing parameters. Use --help to get a list of the possible options."); } else if (args[0].equals("--addPomToTsv")) { if (args.length < 4) logger.error( "Missing arguments for option --addPomToTsv. Please specify <pomFileName> <licenses.stub.tsv> <currentVersion> or use the option --help for further information."); String pomFN = args[1]; String spreadSheetFN = args[2]; String currentVersion = args[3]; MavenProject project = null; try { project = Utils.readPom(new File(pomFN)); } catch (XmlPullParserException e) { logger.error("Could not parse pom file: \"" + pomFN + "\""); } LicensingList licensingList = new LicensingList(); File f = new File(spreadSheetFN); if (f.exists() && !f.isDirectory()) { licensingList.readFromSpreadsheet(spreadSheetFN); } licensingList.addMavenProject(project, currentVersion); licensingList.writeToSpreadsheet(spreadSheetFN); } else if (args[0].equals("--writeLicense3rdParty")) { if (args.length < 4) logger.error( "Missing arguments for option --writeLicense3rdParty. Please provide <licenses.enhanced.tsv> <processModule> <currentVersion> [and <targetDir>] or use the option --help for further information."); String spreadSheetFN = args[1]; String processModule = args[2]; String currentVersion = args[3]; HashMap<String, String> targetDirs = new HashMap<>(); if (args.length > 4) { File targetDir = new File(args[4]); logger.info("scan pom files in direct subdirectories of \"" + targetDir.getPath() + "\" to obtain target locations for 3rd party license files..."); File[] subdirs = targetDir.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY); for (File subdir : subdirs) { File pomFile = new File(subdir.getPath() + File.separator + POM_FN); if (!pomFile.exists()) continue; MavenProject mavenProject; try { mavenProject = Utils.readPom(pomFile); } catch (Exception e) { logger.warn("Could not read from pom file: \"" + pomFile.getPath() + "\" because of " + e.getMessage()); continue; } targetDirs.put(mavenProject.getModel().getArtifactId(), subdir.getAbsolutePath()); } } LicensingList licensingList = new LicensingList(); licensingList.readFromSpreadsheet(spreadSheetFN); if (processModule.toUpperCase().equals("ALL")) { for (String module : licensingList.getNonFixedHeaders()) { try { writeLicense3rdPartyFile(module, licensingList, currentVersion, targetDirs.get(module)); } catch (NoLicenseTemplateSetException e) { logger.error("Could not write file for module \"" + module + "\". There is no template specified for \"" + e.getLicensingObject() + "\". Please add an existing template filename to the column \"" + LicensingObject.ColumnHeader.LICENSE_TEMPLATE.value() + "\" of \"" + spreadSheetFN + "\"."); } } } else { try { writeLicense3rdPartyFile(processModule, licensingList, currentVersion, targetDirs.get(processModule)); } catch (NoLicenseTemplateSetException e) { logger.error("Could not write file for module \"" + processModule + "\". There is no template specified for \"" + e.getLicensingObject() + "\". Please add an existing template filename to the column \"" + LicensingObject.ColumnHeader.LICENSE_TEMPLATE.value() + "\" of \"" + spreadSheetFN + "\"."); } } } else if (args[0].equals("--buildEffectivePom")) { Utils.writeEffectivePom(new File(args[1]), (new File(EFFECTIVE_POM_FN)).getAbsolutePath()); } else if (args[0].equals("--updateTsvWithProjectsInFolder")) { if (args.length < 4) logger.error( "Missing arguments for option --processProjectsInFolder. Please provide <superDirectory> <licenses.stub.tsv> and <currentVersion> or use the option --help for further information."); File directory = new File(args[1]); String spreadSheetFN = args[2]; String currentVersion = args[3]; LicensingList licensingList = new LicensingList(); File f = new File(spreadSheetFN); if (f.exists() && !f.isDirectory()) { licensingList.readFromSpreadsheet(spreadSheetFN); } licensingList.addAll(processProjectsInFolder(directory, currentVersion, false)); licensingList.writeToSpreadsheet(spreadSheetFN); } else if (args[0].equals("--purgeTsv")) { if (args.length < 3) logger.error( "Missing arguments for option --purgeTsv. Please provide <spreadSheetIN.tsv>, <spreadSheetOUT.tsv> and <currentVersion> or use the option --help for further information."); String spreadSheetIN = args[1]; String spreadSheetOUT = args[2]; String currentVersion = args[3]; LicensingList licensingList = new LicensingList(); licensingList.readFromSpreadsheet(spreadSheetIN); licensingList.purge(currentVersion); licensingList.writeToSpreadsheet(spreadSheetOUT); } else if (args[0].equals("--help")) { InputStream in = LicenseUtil.class.getClassLoader().getResourceAsStream(README_PATH); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } else { logger.error("Unknown parameter: " + args[0] + ". Use --help to get a list of the possible options."); } } public static LicensingList processProjectsInFolder(File directory, String currentVersion, Boolean mavenProjectsOnly) throws IOException { LicensingList result = new LicensingList(); File[] subdirs = directory.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY); File gitDir = new File(directory.getPath() + File.separator + ".git"); File pomFile = new File(directory.getPath() + File.separator + "pom.xml"); if (mavenProjectsOnly) { // check pom.xml if (!pomFile.exists()) { return result; } } else { // check git and update if (gitDir.exists()) { logger.info("update local repository"); Utils.updateRepository(directory.getPath()); } for (File dir : subdirs) { result.addAll(processProjectsInFolder(dir, currentVersion, true)); } //return result; } logger.info("process directory: " + directory.getName()); // check git and update if (gitDir.exists()) { logger.info("update local repository"); Utils.updateRepository(directory.getPath()); } if (pomFile.exists()) { logger.info("build effective-pom"); File pom = new File(directory.getPath() + File.separator + EFFECTIVE_POM_FN); Utils.writeEffectivePom(new File(directory.getPath()), pom.getAbsolutePath()); MavenProject project = null; try { project = Utils.readPom(pom); } catch (Exception e) { logger.warn("Could not read from pom file: \"" + pom.getPath() + "\" because of " + e.getMessage()); return result; } FileUtils.delete(pom); // death first for (String module : project.getModules()) { File subdirectory = new File(directory + File.separator + module); result.addAll(processProjectsInFolder(subdirectory, currentVersion, true)); } // add all licence objects of child modules for (LicensingObject licensingObject : result) { licensingObject.put(project.getArtifactId(), currentVersion); } result.addMavenProject(project, currentVersion); } return result; } public static void writeLicense3rdPartyFile(String module, LicensingList licensingList, String currentVersion, String targetDir) throws IOException, NoLicenseTemplateSetException { if (targetDir != null) { File new3rdPartyLicenseFile = new File(targetDir + File.separator + LICENSE_3RD_PARTY_FN); Utils.updateRepository(targetDir); boolean exists = new3rdPartyLicenseFile.exists(); Utils.write(licensingList.getRepoLicensesForModule(module, currentVersion), new3rdPartyLicenseFile); if (!exists) { Utils.addToRepository(targetDir, LICENSE_3RD_PARTY_FN); //Utils.commitAndPushRepository(targetDir, "added file: "+ LICENSE_3RD_PARTY_FN); } else { //Utils.commitAndPushRepository(targetDir, "updated file: "+ LICENSE_3RD_PARTY_FN); } } else { File new3rdPartyLicenseFile = new File(LICENSE_3RD_PARTY_FN + "." + module); Utils.write(licensingList.getRepoLicensesForModule(module, currentVersion), new3rdPartyLicenseFile); } } }