|
| 1 | +package com.github.ahatius.maven; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Collection; |
| 8 | + |
| 9 | +import javax.xml.parsers.DocumentBuilder; |
| 10 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 11 | +import javax.xml.parsers.ParserConfigurationException; |
| 12 | + |
| 13 | +import org.apache.commons.io.FileUtils; |
| 14 | +import org.apache.commons.io.filefilter.DirectoryFileFilter; |
| 15 | +import org.apache.commons.io.filefilter.RegexFileFilter; |
| 16 | +import org.w3c.dom.Document; |
| 17 | +import org.xml.sax.SAXException; |
| 18 | + |
| 19 | +import com.beust.jcommander.JCommander; |
| 20 | +import com.beust.jcommander.Parameter; |
| 21 | +import com.beust.jcommander.ParameterException; |
| 22 | + |
| 23 | +public class DeploymentGenerator { |
| 24 | + public DeploymentGenerator(String[] args) throws ParserConfigurationException, IOException, SAXException { |
| 25 | + // Parse arguments and validate them |
| 26 | + Arguments arguments = new Arguments(); |
| 27 | + JCommander cli = new JCommander(arguments); |
| 28 | + |
| 29 | + try { |
| 30 | + cli.parse(args); |
| 31 | + } catch (ParameterException e) { |
| 32 | + // Display help and exit |
| 33 | + printHelp(cli, e.getMessage()); |
| 34 | + System.exit(1); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + Collection<File> files = FileUtils.listFiles(arguments.dir, new RegexFileFilter("^(.*?)\\.pom"), |
| 39 | + DirectoryFileFilter.DIRECTORY); |
| 40 | + |
| 41 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 42 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 43 | + |
| 44 | + BufferedWriter writer = new BufferedWriter(new FileWriter(arguments.output)); |
| 45 | + try { |
| 46 | + for (File file : files) { |
| 47 | + Document document = documentBuilder.parse(file); |
| 48 | + |
| 49 | + String groupId = document.getElementsByTagName("groupId").item(0).getTextContent(); |
| 50 | + String artifactId = document.getElementsByTagName("artifactId").item(0).getTextContent(); |
| 51 | + String version = document.getElementsByTagName("version").item(0).getTextContent(); |
| 52 | + |
| 53 | + File artifact = new File(file.getParent(), artifactId + "-" + version + ".jar"); |
| 54 | + File sources = new File(file.getParent(), artifactId + "-" + version + "-sources.jar"); |
| 55 | + File javadoc = new File(file.getParent(), artifactId + "-" + version + "-javadoc.jar"); |
| 56 | + |
| 57 | + StringBuilder sb = new StringBuilder(); |
| 58 | + |
| 59 | + sb.append("mvn deploy:deploy-file -DgeneratePom=false "); |
| 60 | + sb.append("-Durl="); |
| 61 | + sb.append(arguments.url); |
| 62 | + sb.append(" "); |
| 63 | + sb.append("-DgroupId="); |
| 64 | + sb.append(groupId); |
| 65 | + sb.append(" "); |
| 66 | + sb.append("-DartifactId="); |
| 67 | + sb.append(artifactId); |
| 68 | + sb.append(" "); |
| 69 | + sb.append("-Dversion="); |
| 70 | + sb.append(version); |
| 71 | + sb.append(" "); |
| 72 | + sb.append("-DpomFile="); |
| 73 | + sb.append(file.getAbsolutePath()); |
| 74 | + sb.append(" "); |
| 75 | + sb.append("-Dfile="); |
| 76 | + sb.append(artifact.getAbsolutePath()); |
| 77 | + sb.append(" "); |
| 78 | + sb.append("-DrepositoryId="); |
| 79 | + sb.append(arguments.repositoryId); |
| 80 | + |
| 81 | + if (sources.exists()) { |
| 82 | + sb.append(" "); |
| 83 | + sb.append("-Dsources="); |
| 84 | + sb.append(sources.getAbsolutePath()); |
| 85 | + } |
| 86 | + |
| 87 | + if (javadoc.exists()) { |
| 88 | + sb.append(" "); |
| 89 | + sb.append("-javadoc="); |
| 90 | + sb.append(javadoc.getAbsolutePath()); |
| 91 | + } |
| 92 | + |
| 93 | + writer.write(sb.toString()); |
| 94 | + writer.newLine(); |
| 95 | + } |
| 96 | + } finally { |
| 97 | + writer.close(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Main method |
| 103 | + * |
| 104 | + * @param args Commandline arguments |
| 105 | + */ |
| 106 | + public static void main(String[] args) |
| 107 | + throws IOException, SAXException, ParserConfigurationException { |
| 108 | + new DeploymentGenerator(args); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Prints the help text including a message what went wrong |
| 113 | + * |
| 114 | + * @param cli The JCommander object that will be used to generate the help |
| 115 | + * @param message Additional information that shows why the help is shown |
| 116 | + */ |
| 117 | + private static void printHelp(JCommander cli, String message) { |
| 118 | + System.out.println(); |
| 119 | + System.out.println(message); |
| 120 | + System.out.println(); |
| 121 | + cli.usage(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Contains the allowed and required arguments for this application |
| 126 | + */ |
| 127 | + private static class Arguments { |
| 128 | + @Parameter(names = {"-d"}, description = "The directory to scan for pom files", required = true) |
| 129 | + private File dir; |
| 130 | + |
| 131 | + @Parameter(names = {"-r"}, |
| 132 | + description = "The target repository id that is configured in the settings.xml", |
| 133 | + required = true) |
| 134 | + private String repositoryId; |
| 135 | + |
| 136 | + @Parameter(names = {"-u"}, description = "The target deployment URL of the repository", |
| 137 | + required = true) |
| 138 | + private String url; |
| 139 | + |
| 140 | + @Parameter(names = {"-o"}, |
| 141 | + description = "The shell script that should be generated by this application", |
| 142 | + required = true) |
| 143 | + private File output; |
| 144 | + } |
| 145 | +} |
0 commit comments