Search in sources :

Example 41 with Project

use of org.apache.tools.ant.Project in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2PojoTaskIT method invokeAntBuild.

private void invokeAntBuild(String pathToBuildFile) throws URISyntaxException {
    File buildFile = new File(this.getClass().getResource(pathToBuildFile).toURI());
    Project project = new Project();
    project.setUserProperty("ant.file", buildFile.getAbsolutePath());
    project.setUserProperty("targetDirectory", schemaRule.getGenerateDir().getAbsolutePath());
    project.init();
    ProjectHelper helper = ProjectHelper.getProjectHelper();
    project.addReference("ant.projecthelper", helper);
    helper.parse(project, buildFile);
    project.executeTarget(project.getDefaultTarget());
}
Also used : Project(org.apache.tools.ant.Project) ProjectHelper(org.apache.tools.ant.ProjectHelper) File(java.io.File)

Example 42 with Project

use of org.apache.tools.ant.Project in project hudson-2.x by hudson.

the class ClassicPluginStrategy method parseClassPath.

private static void parseClassPath(Manifest manifest, File archive, List<File> paths, String attributeName, String separator) throws IOException {
    String classPath = manifest.getMainAttributes().getValue(attributeName);
    // attribute not found
    if (classPath == null)
        return;
    for (String s : classPath.split(separator)) {
        File file = resolve(archive, s);
        if (file.getName().contains("*")) {
            // handle wildcard
            FileSet fs = new FileSet();
            File dir = file.getParentFile();
            fs.setDir(dir);
            fs.setIncludes(file.getName());
            for (String included : fs.getDirectoryScanner(new Project()).getIncludedFiles()) {
                paths.add(new File(dir, included));
            }
        } else {
            if (!file.exists())
                throw new IOException("No such file: " + file);
            paths.add(file);
        }
    }
}
Also used : Project(org.apache.tools.ant.Project) FileSet(org.apache.tools.ant.types.FileSet) IOException(java.io.IOException) File(java.io.File)

Example 43 with Project

use of org.apache.tools.ant.Project in project hudson-2.x by hudson.

the class FilePath method validateAntFileMask.

/**
     * Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar")
     * against this directory, and try to point out the problem.
     *
     * <p>
     * This is useful in conjunction with {@link FormValidation}.
     *
     * @return
     *      null if no error was found. Otherwise returns a human readable error message.
     * @since 1.90
     * @see #validateFileMask(FilePath, String)
     */
public String validateAntFileMask(final String fileMasks) throws IOException, InterruptedException {
    return act(new FileCallable<String>() {

        public String invoke(File dir, VirtualChannel channel) throws IOException {
            if (fileMasks.startsWith("~"))
                return Messages.FilePath_TildaDoesntWork();
            StringTokenizer tokens = new StringTokenizer(fileMasks, ",");
            while (tokens.hasMoreTokens()) {
                final String fileMask = tokens.nextToken().trim();
                if (hasMatch(dir, fileMask))
                    // no error on this portion
                    continue;
                // so see if we can match by using ' ' as the separator
                if (fileMask.contains(" ")) {
                    boolean matched = true;
                    for (String token : Util.tokenize(fileMask)) matched &= hasMatch(dir, token);
                    if (matched)
                        return Messages.FilePath_validateAntFileMask_whitespaceSeprator();
                }
                // a common mistake is to assume the wrong base dir, and there are two variations
                // to this: (1) the user gave us aa/bb/cc/dd where cc/dd was correct
                // and (2) the user gave us cc/dd where aa/bb/cc/dd was correct.
                {
                    // check the (1) above first
                    String f = fileMask;
                    while (true) {
                        int idx = findSeparator(f);
                        if (idx == -1)
                            break;
                        f = f.substring(idx + 1);
                        if (hasMatch(dir, f))
                            return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, f);
                    }
                }
                {
                    // check the (2) above next as this is more expensive.
                    // Try prepending "**/" to see if that results in a match
                    FileSet fs = Util.createFileSet(dir, "**/" + fileMask);
                    DirectoryScanner ds = fs.getDirectoryScanner(new Project());
                    if (ds.getIncludedFilesCount() != 0) {
                        // try shorter name first so that the suggestion results in least amount of changes
                        String[] names = ds.getIncludedFiles();
                        Arrays.sort(names, SHORTER_STRING_FIRST);
                        for (String f : names) {
                            // now we want to decompose f to the leading portion that matched "**"
                            // and the trailing portion that matched the file mask, so that
                            // we can suggest the user error.
                            //
                            // this is not a very efficient/clever way to do it, but it's relatively simple
                            String prefix = "";
                            while (true) {
                                int idx = findSeparator(f);
                                if (idx == -1)
                                    break;
                                prefix += f.substring(0, idx) + '/';
                                f = f.substring(idx + 1);
                                if (hasMatch(dir, prefix + fileMask))
                                    return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, prefix + fileMask);
                            }
                        }
                    }
                }
                {
                    // finally, see if we can identify any sub portion that's valid. Otherwise bail out
                    String previous = null;
                    String pattern = fileMask;
                    while (true) {
                        if (hasMatch(dir, pattern)) {
                            // found a match
                            if (previous == null)
                                return Messages.FilePath_validateAntFileMask_portionMatchAndSuggest(fileMask, pattern);
                            else
                                return Messages.FilePath_validateAntFileMask_portionMatchButPreviousNotMatchAndSuggest(fileMask, pattern, previous);
                        }
                        int idx = findSeparator(pattern);
                        if (idx < 0) {
                            // no more path component left to go back
                            if (pattern.equals(fileMask))
                                return Messages.FilePath_validateAntFileMask_doesntMatchAnything(fileMask);
                            else
                                return Messages.FilePath_validateAntFileMask_doesntMatchAnythingAndSuggest(fileMask, pattern);
                        }
                        // cut off the trailing component and try again
                        previous = pattern;
                        pattern = pattern.substring(0, idx);
                    }
                }
            }
            // no error
            return null;
        }

        private boolean hasMatch(File dir, String pattern) {
            FileSet fs = Util.createFileSet(dir, pattern);
            DirectoryScanner ds = fs.getDirectoryScanner(new Project());
            return ds.getIncludedFilesCount() != 0 || ds.getIncludedDirsCount() != 0;
        }

        /**
             * Finds the position of the first path separator.
             */
        private int findSeparator(String pattern) {
            int idx1 = pattern.indexOf('\\');
            int idx2 = pattern.indexOf('/');
            if (idx1 == -1)
                return idx2;
            if (idx2 == -1)
                return idx1;
            return Math.min(idx1, idx2);
        }
    });
}
Also used : Project(org.apache.tools.ant.Project) AbstractProject(hudson.model.AbstractProject) StringTokenizer(java.util.StringTokenizer) FileSet(org.apache.tools.ant.types.FileSet) VirtualChannel(hudson.remoting.VirtualChannel) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException) File(java.io.File)

Example 44 with Project

use of org.apache.tools.ant.Project in project che by eclipse.

the class AntUtils method readProject.

/**
     * Read description of ant project.
     *
     * @param buildFile
     *         path to build.xml file
     * @return description of ant project
     */
public static Project readProject(java.io.File buildFile) throws IOException {
    org.apache.tools.ant.Project antProject = new org.apache.tools.ant.Project();
    //TODO: try fix problem with some build.xml that don't have basedir prop
    antProject.setBasedir(buildFile.getParentFile().getAbsolutePath());
    try {
        ProjectHelper2.configureProject(antProject, buildFile);
    } catch (Exception e) {
        //TODO: return empty project. Skip all parsing error. Lets importing project if not parse build.xml. In this case will used default props.
        return antProject;
    }
    return antProject;
}
Also used : Project(org.apache.tools.ant.Project) Project(org.apache.tools.ant.Project) IOException(java.io.IOException)

Example 45 with Project

use of org.apache.tools.ant.Project in project flyway by flyway.

the class AntLog method debug.

public void debug(String message) {
    Project antProject = AntLogCreator.INSTANCE.getAntProject();
    Task task = antProject.getThreadTask(Thread.currentThread());
    antProject.log(task, message, Project.MSG_VERBOSE);
}
Also used : Project(org.apache.tools.ant.Project) Task(org.apache.tools.ant.Task)

Aggregations

Project (org.apache.tools.ant.Project)80 File (java.io.File)35 BuildException (org.apache.tools.ant.BuildException)23 IOException (java.io.IOException)17 Test (org.junit.Test)13 FileSet (org.apache.tools.ant.types.FileSet)11 ZipFile (java.util.zip.ZipFile)10 IgniteDeploymentGarAntTask (org.apache.ignite.util.antgar.IgniteDeploymentGarAntTask)8 Path (org.apache.tools.ant.types.Path)6 DefaultLogger (org.apache.tools.ant.DefaultLogger)5 ProjectHelper (org.apache.tools.ant.ProjectHelper)5 MissingMethodException (groovy.lang.MissingMethodException)4 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 Task (org.apache.tools.ant.Task)4 AbstractProject (hudson.model.AbstractProject)3 Target (org.apache.tools.ant.Target)3 Zip (org.apache.tools.ant.taskdefs.Zip)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 Binding (groovy.lang.Binding)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)2