Search in sources :

Example 11 with BuildException

use of org.apache.tools.ant.BuildException in project lucene-solr by apache.

the class GetMavenDependenciesTask method getTransitiveDependenciesFromIvyCache.

/**
   * Collect transitive compile-scope dependencies for the given artifact's
   * ivy.xml from the Ivy cache, using the default ivy pattern 
   * "[organisation]/[module]/ivy-[revision].xml".  See 
   * <a href="http://ant.apache.org/ivy/history/latest-milestone/settings/caches.html"
   * >the Ivy cache documentation</a>.
   */
private Collection<String> getTransitiveDependenciesFromIvyCache(String groupId, String artifactId, String version) {
    SortedSet<String> transitiveDependencies = new TreeSet<>();
    //                                      E.g. ~/.ivy2/cache/xerces/xercesImpl/ivy-2.9.1.xml
    File ivyXmlFile = new File(new File(new File(ivyCacheDir, groupId), artifactId), "ivy-" + version + ".xml");
    if (!ivyXmlFile.exists()) {
        throw new BuildException("File not found: " + ivyXmlFile.getPath());
    }
    try {
        Document document = documentBuilder.parse(ivyXmlFile);
        String dependencyPath = "/ivy-module/dependencies/dependency" + "[   not(starts-with(@conf,'test->'))" + "and not(starts-with(@conf,'provided->'))" + "and not(starts-with(@conf,'optional->'))]";
        NodeList dependencies = (NodeList) xpath.evaluate(dependencyPath, document, XPathConstants.NODESET);
        for (int i = 0; i < dependencies.getLength(); ++i) {
            Element dependency = (Element) dependencies.item(i);
            transitiveDependencies.add(dependency.getAttribute("org") + ':' + dependency.getAttribute("name"));
        }
    } catch (Exception e) {
        throw new BuildException("Exception collecting transitive dependencies for " + groupId + ':' + artifactId + ':' + version + " from " + ivyXmlFile.getAbsolutePath(), e);
    }
    return transitiveDependencies;
}
Also used : TreeSet(java.util.TreeSet) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) BuildException(org.apache.tools.ant.BuildException) Document(org.w3c.dom.Document) File(java.io.File) XPathExpressionException(javax.xml.xpath.XPathExpressionException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 12 with BuildException

use of org.apache.tools.ant.BuildException in project lucene-solr by apache.

the class GetMavenDependenciesTask method getIvyCacheDir.

/**
   * Sets the ivyCacheDir field, to either the ${ivy.default.ivy.user.dir} 
   * property, or if that's not set, to the default ~/.ivy2/.
   */
private File getIvyCacheDir() {
    String ivyUserDirName = getProject().getUserProperty(IVY_USER_DIR_PROPERTY);
    if (null == ivyUserDirName) {
        ivyUserDirName = getProject().getProperty(IVY_USER_DIR_PROPERTY);
        if (null == ivyUserDirName) {
            ivyUserDirName = System.getProperty("user.home") + System.getProperty("file.separator") + ".ivy2";
        }
    }
    File ivyUserDir = new File(ivyUserDirName);
    if (!ivyUserDir.exists()) {
        throw new BuildException("Ivy user dir does not exist: '" + ivyUserDir.getPath() + "'");
    }
    File dir = new File(ivyUserDir, "cache");
    if (!dir.exists()) {
        throw new BuildException("Ivy cache dir does not exist: '" + ivyCacheDir.getPath() + "'");
    }
    return dir;
}
Also used : BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 13 with BuildException

use of org.apache.tools.ant.BuildException in project lucene-solr by apache.

the class GetMavenDependenciesTask method getModuleName.

/**
   * Extract module name from ivy.xml path.
   */
private String getModuleName(File ivyXmlFile) {
    String path = ivyXmlFile.getAbsolutePath();
    Matcher matcher = PROPERTY_PREFIX_FROM_IVY_XML_FILE_PATTERN.matcher(path);
    if (!matcher.find()) {
        throw new BuildException("Can't get module name from ivy.xml path: " + path);
    }
    StringBuilder builder = new StringBuilder();
    builder.append(matcher.group(1));
    if (null != matcher.group(2)) {
        // "lucene/analysis/..."
        builder.append("-analyzers");
    } else if (null != matcher.group(3)) {
        // "solr/example/..."
        builder.append("-example");
    } else if (null != matcher.group(4)) {
        // "solr/server/..."
        builder.append("-server");
    }
    builder.append('-');
    builder.append(matcher.group(5));
    return builder.toString().replace("solr-solr-", "solr-");
}
Also used : Matcher(java.util.regex.Matcher) BuildException(org.apache.tools.ant.BuildException)

Example 14 with BuildException

use of org.apache.tools.ant.BuildException in project lucene-solr by apache.

the class GetMavenDependenciesTask method setInternalDependencyProperties.

/**
   * Sets the internal dependencies compile and test properties to be inserted 
   * into modules' POMs.
   * 
   * Also collects shared external dependencies, 
   * e.g. solr-core wants all of solrj's external dependencies 
   */
private void setInternalDependencyProperties() {
    log("Loading module dependencies from: " + moduleDependenciesPropertiesFile, verboseLevel);
    Properties moduleDependencies = new Properties();
    try (InputStream inputStream = new FileInputStream(moduleDependenciesPropertiesFile);
        Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
        moduleDependencies.load(reader);
    } catch (FileNotFoundException e) {
        throw new BuildException("Properties file does not exist: " + moduleDependenciesPropertiesFile.getPath());
    } catch (IOException e) {
        throw new BuildException("Exception reading properties file " + moduleDependenciesPropertiesFile.getPath(), e);
    }
    Map<String, SortedSet<String>> testScopeDependencies = new HashMap<>();
    Map<String, String> testScopePropertyKeys = new HashMap<>();
    for (Map.Entry<?, ?> entry : moduleDependencies.entrySet()) {
        String newPropertyKey = (String) entry.getKey();
        StringBuilder newPropertyValue = new StringBuilder();
        String value = (String) entry.getValue();
        Matcher matcher = MODULE_DEPENDENCIES_COORDINATE_KEY_PATTERN.matcher(newPropertyKey);
        if (!matcher.matches()) {
            throw new BuildException("Malformed module dependencies property key: '" + newPropertyKey + "'");
        }
        String antProjectName = matcher.group(1);
        boolean isTest = null != matcher.group(2);
        String artifactName = antProjectToArtifactName(antProjectName);
        // Add ".internal"
        newPropertyKey = artifactName + (isTest ? ".internal.test" : ".internal") + ".dependencies";
        if (isTest) {
            testScopePropertyKeys.put(artifactName, newPropertyKey);
        }
        if (null == value || value.isEmpty()) {
            allProperties.setProperty(newPropertyKey, "");
            Map<String, SortedSet<String>> scopedDependencies = isTest ? testScopeDependencies : internalCompileScopeDependencies;
            scopedDependencies.put(artifactName, new TreeSet<String>());
        } else {
            // Lucene analysis modules' build dirs do not include hyphens, but Solr contribs' build dirs do
            String origModuleDir = antProjectName.replace("analyzers-", "analysis/");
            // Exclude the module's own build output, in addition to UNWANTED_INTERNAL_DEPENDENCIES
            Pattern unwantedInternalDependencies = Pattern.compile(// require dir separator 
            "(?:lucene/build/|solr/build/(?:contrib/)?)" + origModuleDir + "/" + "|" + UNWANTED_INTERNAL_DEPENDENCIES);
            SortedSet<String> sortedDeps = new TreeSet<>();
            for (String dependency : value.split(",")) {
                matcher = SHARED_EXTERNAL_DEPENDENCIES_PATTERN.matcher(dependency);
                if (matcher.find()) {
                    String otherArtifactName = matcher.group(1);
                    boolean isTestScope = null != matcher.group(2) && matcher.group(2).length() > 0;
                    otherArtifactName = otherArtifactName.replace('/', '-');
                    otherArtifactName = otherArtifactName.replace("lucene-analysis", "lucene-analyzers");
                    otherArtifactName = otherArtifactName.replace("solr-contrib-solr-", "solr-");
                    otherArtifactName = otherArtifactName.replace("solr-contrib-", "solr-");
                    if (!otherArtifactName.equals(artifactName)) {
                        Map<String, Set<String>> sharedDeps = isTest ? interModuleExternalTestScopeDependencies : interModuleExternalCompileScopeDependencies;
                        Set<String> sharedSet = sharedDeps.get(artifactName);
                        if (null == sharedSet) {
                            sharedSet = new HashSet<>();
                            sharedDeps.put(artifactName, sharedSet);
                        }
                        if (isTestScope) {
                            otherArtifactName += ":test";
                        }
                        sharedSet.add(otherArtifactName);
                    }
                }
                matcher = unwantedInternalDependencies.matcher(dependency);
                if (matcher.find()) {
                    // skip external (/(test-)lib/), and non-jar and unwanted (self) internal deps
                    continue;
                }
                String artifactId = dependencyToArtifactId(newPropertyKey, dependency);
                String groupId = ivyModuleInfo.get(artifactId);
                String coordinate = groupId + ':' + artifactId;
                sortedDeps.add(coordinate);
            }
            if (isTest) {
                // Don't set test-scope properties until all compile-scope deps have been seen
                testScopeDependencies.put(artifactName, sortedDeps);
            } else {
                internalCompileScopeDependencies.put(artifactName, sortedDeps);
                for (String dependency : sortedDeps) {
                    int splitPos = dependency.indexOf(':');
                    String groupId = dependency.substring(0, splitPos);
                    String artifactId = dependency.substring(splitPos + 1);
                    appendDependencyXml(newPropertyValue, groupId, artifactId, "    ", null, false, false, null, null);
                }
                if (newPropertyValue.length() > 0) {
                    // drop trailing newline
                    newPropertyValue.setLength(newPropertyValue.length() - 1);
                }
                allProperties.setProperty(newPropertyKey, newPropertyValue.toString());
            }
        }
    }
    // dependencies that are not also compile-scope dependencies.
    for (Map.Entry<String, SortedSet<String>> entry : testScopeDependencies.entrySet()) {
        String module = entry.getKey();
        SortedSet<String> testDeps = entry.getValue();
        SortedSet<String> compileDeps = internalCompileScopeDependencies.get(module);
        if (null == compileDeps) {
            throw new BuildException("Can't find compile scope dependencies for module " + module);
        }
        StringBuilder newPropertyValue = new StringBuilder();
        for (String dependency : testDeps) {
            // included in their test-scope deps.
            if (modulesWithSeparateCompileAndTestPOMs.contains(module) || !compileDeps.contains(dependency)) {
                int splitPos = dependency.indexOf(':');
                String groupId = dependency.substring(0, splitPos);
                String artifactId = dependency.substring(splitPos + 1);
                appendDependencyXml(newPropertyValue, groupId, artifactId, "    ", null, true, false, null, null);
            }
        }
        if (newPropertyValue.length() > 0) {
            // drop trailing newline
            newPropertyValue.setLength(newPropertyValue.length() - 1);
        }
        allProperties.setProperty(testScopePropertyKeys.get(module), newPropertyValue.toString());
    }
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 15 with BuildException

use of org.apache.tools.ant.BuildException in project lucene-solr by apache.

the class LicenseCheckTask method checkJarFile.

/**
   * Check a single JAR file.
   */
private boolean checkJarFile(File jarFile) {
    log("Scanning: " + jarFile.getPath(), verboseLevel);
    if (!skipChecksum) {
        boolean skipDueToSnapshot = skipSnapshotsChecksum && jarFile.getName().contains("-SNAPSHOT");
        if (!skipDueToSnapshot && !matchesRegexChecksum(jarFile, skipRegexChecksum)) {
            // validate the jar matches against our expected hash
            final File checksumFile = new File(licenseDirectory, jarFile.getName() + "." + CHECKSUM_TYPE);
            if (!(checksumFile.exists() && checksumFile.canRead())) {
                log("MISSING " + CHECKSUM_TYPE + " checksum file for: " + jarFile.getPath(), Project.MSG_ERR);
                log("EXPECTED " + CHECKSUM_TYPE + " checksum file : " + checksumFile.getPath(), Project.MSG_ERR);
                this.failures = true;
                return false;
            } else {
                final String expectedChecksum = readChecksumFile(checksumFile);
                try {
                    final MessageDigest md = MessageDigest.getInstance(CHECKSUM_TYPE);
                    byte[] buf = new byte[CHECKSUM_BUFFER_SIZE];
                    try {
                        FileInputStream fis = new FileInputStream(jarFile);
                        try {
                            DigestInputStream dis = new DigestInputStream(fis, md);
                            try {
                                while (dis.read(buf, 0, CHECKSUM_BUFFER_SIZE) != -1) {
                                // NOOP
                                }
                            } finally {
                                dis.close();
                            }
                        } finally {
                            fis.close();
                        }
                    } catch (IOException ioe) {
                        throw new BuildException("IO error computing checksum of file: " + jarFile, ioe);
                    }
                    final byte[] checksumBytes = md.digest();
                    final String checksum = createChecksumString(checksumBytes);
                    if (!checksum.equals(expectedChecksum)) {
                        log("CHECKSUM FAILED for " + jarFile.getPath() + " (expected: \"" + expectedChecksum + "\" was: \"" + checksum + "\")", Project.MSG_ERR);
                        this.failures = true;
                        return false;
                    }
                } catch (NoSuchAlgorithmException ae) {
                    throw new BuildException("Digest type " + CHECKSUM_TYPE + " not supported by your JVM", ae);
                }
            }
        } else if (skipDueToSnapshot) {
            log("Skipping jar because it is a SNAPSHOT : " + jarFile.getAbsolutePath(), Project.MSG_INFO);
        } else {
            log("Skipping jar because it matches regex pattern: " + jarFile.getAbsolutePath() + " pattern: " + skipRegexChecksum.pattern(), Project.MSG_INFO);
        }
    }
    // Get the expected license path base from the mapper and search for license files.
    Map<File, LicenseType> foundLicenses = new LinkedHashMap<>();
    List<File> expectedLocations = new ArrayList<>();
    outer: for (String mappedPath : licenseMapper.mapFileName(jarFile.getName())) {
        for (LicenseType licenseType : LicenseType.values()) {
            File licensePath = new File(licenseDirectory, mappedPath + licenseType.licenseFileSuffix());
            if (licensePath.exists()) {
                foundLicenses.put(licensePath, licenseType);
                log(" FOUND " + licenseType.name() + " license at " + licensePath.getPath(), verboseLevel);
                // We could continue scanning here to detect duplicate associations?
                break outer;
            } else {
                expectedLocations.add(licensePath);
            }
        }
    }
    // Check for NOTICE files.
    for (Map.Entry<File, LicenseType> e : foundLicenses.entrySet()) {
        LicenseType license = e.getValue();
        String licensePath = e.getKey().getName();
        String baseName = licensePath.substring(0, licensePath.length() - license.licenseFileSuffix().length());
        File noticeFile = new File(licenseDirectory, baseName + license.noticeFileSuffix());
        if (noticeFile.exists()) {
            log(" FOUND NOTICE file at " + noticeFile.getAbsolutePath(), verboseLevel);
        } else {
            if (license.isNoticeRequired()) {
                this.failures = true;
                log("MISSING NOTICE for the license file:\n  " + licensePath + "\n  Expected location below:\n  " + noticeFile.getAbsolutePath(), Project.MSG_ERR);
            }
        }
    }
    // In case there is something missing, complain.
    if (foundLicenses.isEmpty()) {
        this.failures = true;
        StringBuilder message = new StringBuilder();
        message.append("MISSING LICENSE for the following file:\n  " + jarFile.getAbsolutePath() + "\n  Expected locations below:\n");
        for (File location : expectedLocations) {
            message.append("  => ").append(location.getAbsolutePath()).append("\n");
        }
        log(message.toString(), Project.MSG_ERR);
        return false;
    }
    return true;
}
Also used : DigestInputStream(java.security.DigestInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) BuildException(org.apache.tools.ant.BuildException) MessageDigest(java.security.MessageDigest) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

BuildException (org.apache.tools.ant.BuildException)930 IOException (java.io.IOException)390 File (java.io.File)365 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)75 ArrayList (java.util.ArrayList)65 InputStream (java.io.InputStream)62 Project (org.apache.tools.ant.Project)61 Resource (org.apache.tools.ant.types.Resource)58 FileSet (org.apache.tools.ant.types.FileSet)52 Path (org.apache.tools.ant.types.Path)52 Commandline (org.apache.tools.ant.types.Commandline)51 Properties (java.util.Properties)50 OutputStream (java.io.OutputStream)44 FileOutputStream (java.io.FileOutputStream)42 FileResource (org.apache.tools.ant.types.resources.FileResource)42 FileInputStream (java.io.FileInputStream)41 URL (java.net.URL)40 BufferedReader (java.io.BufferedReader)37 Writer (java.io.Writer)37 MalformedURLException (java.net.MalformedURLException)37