Example usage for org.apache.maven.project MavenProject getTestResources

List of usage examples for org.apache.maven.project MavenProject getTestResources

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getTestResources.

Prototype

public List<Resource> getTestResources() 

Source Link

Usage

From source file:com.sixdegreeshq.sitenav.GeneratorMojo.java

License:Apache License

/**
 * got from//from www.  ja  v a  2s .c o  m
 * https://github.com/querydsl/querydsl/blob/master/querydsl-maven-plugin/src/main/java/com/querydsl/maven/AbstractExporterMojo.java
 *
 */
@SuppressWarnings("unchecked")
private ClassLoader getProjectClassLoader(MavenProject project)
        throws DependencyResolutionRequiredException, MalformedURLException {
    List<String> classpathElements;
    if (testing) {
        classpathElements = project.getTestClasspathElements();
        for (Resource testResource : project.getTestResources()) {
            classpathElements.add(testResource.getDirectory());
        }

    } else {
        classpathElements = project.getCompileClasspathElements();
    }

    for (Resource testResource : project.getResources()) {
        classpathElements.add(testResource.getDirectory());
    }

    List<URL> urls = new ArrayList<URL>(classpathElements.size());
    for (String element : classpathElements) {
        File file = new File(element);
        if (file.exists()) {
            urls.add(file.toURI().toURL());
        }
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
}

From source file:com.totsp.mavenplugin.gwt.util.BuildClasspathUtil.java

License:Open Source License

/**
 * Get resources for specific scope.//from www. j  av a 2  s.  c o  m
 * 
 * @param project
 * @param scope
 * @return
 */
@SuppressWarnings("unchecked")
private static List<Resource> getResources(final MavenProject project, final DependencyScope scope) {
    if (DependencyScope.COMPILE.equals(scope)) {
        return project.getResources();
    } else if (DependencyScope.TEST.equals(scope)) {
        return project.getTestResources();
    } else {
        throw new RuntimeException("Not allowed scope " + scope);
    }
}

From source file:org.apache.myfaces.trinidadbuild.plugin.jdeveloper.JDeveloperMojo.java

License:Apache License

private void generateTestProject() throws IOException, MojoExecutionException {
    if (!"pom".equals(project.getPackaging()) && projectHasTests) {
        File projectFile = getJProjectTestFile(project);

        // Get Project Properties to tell Mojo whether or not to add
        // library refs and taglibs to the project.
        Properties props = project.getProperties();
        String addLibs = (String) props.get(_PROPERTY_ADD_LIBRARY);
        String addTagLibs = (String) props.get(_PROPERTY_ADD_TAGLIBS);
        _addLibraries = (addLibs == null) ? true : (new Boolean(addLibs)).booleanValue();
        _addTagLibs = (addTagLibs == null) ? true : (new Boolean(addTagLibs)).booleanValue();

        File webappDir = new File(project.getBasedir(), "src/test/webapp");
        // TODO: read configuration for compiler:testCompile goal
        File outputDir = new File(project.getBuild().getDirectory(), "test-classes");

        // self dependency needed for test project
        List testDependencies = new ArrayList(project.getTestDependencies());
        Dependency selfDependency = new Dependency();
        selfDependency.setArtifactId(project.getArtifactId());
        selfDependency.setGroupId(project.getGroupId());
        selfDependency.setType(project.getPackaging());
        testDependencies.add(selfDependency);

        MavenProject executionProject = project.getExecutionProject();
        List compileSourceRoots = executionProject.getTestCompileSourceRoots();

        if (testResourceRoots != null) {
            for (int i = 0; i < testSourceRoots.length; i++) {
                compileSourceRoots.add(testSourceRoots[i].getAbsolutePath());
            }//  w w w .  j  a va2 s  . c o m
        }

        List compileResourceRoots = executionProject.getTestResources();
        if (testResourceRoots != null) {
            for (int i = 0; i < testResourceRoots.length; i++) {
                Resource resource = new Resource();
                resource.setDirectory(testSourceRoots[i].getAbsolutePath());
                compileResourceRoots.add(resource);
            }
        }

        getLog().info("Generating JDeveloper " + release + " Project " + project.getArtifactId() + "-test");

        // Note: all artifacts implicitly included in "test" scope
        generateProject(projectFile, project.getArtifactId() + "-test", project.getPackaging(),
                testDependencies, project.getTestArtifacts(), compileSourceRoots, compileResourceRoots,
                Collections.singletonList(webappDir.getPath()), outputDir);
    }
}

From source file:org.apache.usergrid.chop.plugin.Utils.java

License:Apache License

/**
 * Copies all found resource files, including test resources to the <code>targetFolder</code>.
 * <p>/*ww w.  j a v  a 2  s  .c om*/
 * Resource files to be copied are filtered or included according to the configurations inside
 * <code>project</code>'s pom.xml file.
 *
 * @param project       project whose resource files to be copied
 * @param targetFolder  matching resource files are stored in this directory
 * @return
 */
public static boolean copyResourcesTo(MavenProject project, String targetFolder) {
    File targetFolderFile = new File(targetFolder);
    String includes;
    String excludes;
    List allResources = project.getResources();
    allResources.addAll(project.getTestResources());

    // If there is no resource folder under project, mvn chop:runner goal should fail
    if (!hasResourceFolders(project)) {
        return false;
    } else {
        LOG.info("Copying resource files to runner.jar");

        for (Object res : allResources) {
            if (!(res instanceof Resource)) {
                continue;
            }
            Resource resource = (Resource) res;
            try {
                File baseDir = new File(resource.getDirectory());
                includes = resource.getIncludes().toString().replace("[", "").replace("]", "").replace(" ", "");
                excludes = resource.getExcludes().toString().replace("[", "").replace("]", "").replace(" ", "");

                List<String> resFiles = FileUtils.getFileNames(baseDir, includes, excludes, true, true);
                for (String resFile : resFiles) {
                    File resourceFile = new File(resFile);
                    LOG.info("Copying {} to {}", resourceFile.getName(), targetFolder);
                    FileUtils.copyFileToDirectory(resourceFile, targetFolderFile);
                }
            } catch (IOException e) {
                LOG.info("Error while trying to copy resource files.", e);
            } catch (IllegalStateException e) {
                String path = resource.getDirectory();
                path = path.substring(0, path.lastIndexOf("/"));
                LOG.info("There is no resource folder under {} folder.", path);
            }
        }
        return true;
    }
}

From source file:org.codehaus.mojo.gwt.ClasspathBuilder.java

License:Apache License

/**
 * Build classpath list using either gwtHome (if present) or using *project* dependencies. Note that this is ONLY
 * used for the script/cmd writers (so the scopes are not for the compiler, or war plugins, etc). This is required
 * so that the script writers can get the dependencies they need regardless of the Maven scopes (still want to use
 * the Maven scopes for everything else Maven, but for GWT-Maven we need to access deps differently - directly at
 * times).//from  w w w  . j a  v a2 s . c  o m
 *
 * @param project The maven project the Mojo is running for
 * @param artifacts the project artifacts (all scopes)
 * @param scope artifact scope to use
 * @param isGenerator whether to use processed resources and compiled classes (false), or raw resources (true).
 * @return file collection for classpath
 */
public Collection<File> buildClasspathList(final MavenProject project, final String scope,
        Set<Artifact> artifacts, boolean isGenerator) throws ClasspathBuilderException {
    getLogger().debug("establishing classpath list (scope = " + scope + ")");

    Set<File> items = new LinkedHashSet<File>();

    // Note : Don't call addSourceWithActiveProject as a GWT dependency MUST be a valid GWT library module :
    // * include java sources in the JAR as resources
    // * define a gwt.xml module file to declare the required inherits
    // addSourceWithActiveProject would make some java sources available to GWT compiler that should not be accessible in
    // a non-reactor build, making the build less deterministic and encouraging bad design.

    if (!isGenerator) {
        items.add(new File(project.getBuild().getOutputDirectory()));
    }
    addSources(items, project.getCompileSourceRoots());
    if (isGenerator) {
        addResources(items, project.getResources());
    }
    // Use our own ClasspathElements fitering, as for RUNTIME we need to include PROVIDED artifacts,
    // that is not the default Maven policy, as RUNTIME is used here to build the GWTShell execution classpath

    if (scope.equals(SCOPE_TEST)) {
        addSources(items, project.getTestCompileSourceRoots());
        addResources(items, project.getTestResources());
        items.add(new File(project.getBuild().getTestOutputDirectory()));

        // Add all project dependencies in classpath
        for (Artifact artifact : artifacts) {
            items.add(artifact.getFile());
        }
    } else if (scope.equals(SCOPE_COMPILE)) {
        // Add all project dependencies in classpath
        getLogger().debug("candidate artifacts : " + artifacts.size());
        for (Artifact artifact : artifacts) {
            String artifactScope = artifact.getScope();
            if (SCOPE_COMPILE.equals(artifactScope) || SCOPE_PROVIDED.equals(artifactScope)
                    || SCOPE_SYSTEM.equals(artifactScope)) {
                items.add(artifact.getFile());
            }
        }
    } else if (scope.equals(SCOPE_RUNTIME)) {
        // Add all dependencies BUT "TEST" as we need PROVIDED ones to setup the execution
        // GWTShell that is NOT a full JEE server
        for (Artifact artifact : artifacts) {
            getLogger().debug("candidate artifact : " + artifact);
            if (!artifact.getScope().equals(SCOPE_TEST) && artifact.getArtifactHandler().isAddedToClasspath()) {
                items.add(artifact.getFile());
            }
        }
    } else {
        throw new ClasspathBuilderException("unsupported scope " + scope);
    }
    return items;
}

From source file:org.codehaus.mojo.gwt.ClasspathBuilder.java

License:Apache License

/**
 * Get resources for specific scope./*from   w w  w.  j  av  a 2 s . c om*/
 *
 * @param project
 * @param scope
 * @return
 */
private List<Resource> getResources(final MavenProject project, final String scope) {
    if (SCOPE_COMPILE.equals(scope) || SCOPE_RUNTIME.equals(scope)) {
        return project.getResources();
    } else if (SCOPE_TEST.equals(scope)) {
        List<Resource> resources = new ArrayList<Resource>();
        resources.addAll(project.getTestResources());
        resources.addAll(project.getResources());
        return resources;
    } else {
        throw new RuntimeException("Not allowed scope " + scope);
    }
}

From source file:org.codehaus.mojo.gwt.shell.ClasspathBuilder.java

License:Apache License

/**
 * Build classpath list using either gwtHome (if present) or using *project* dependencies. Note that this is ONLY
 * used for the script/cmd writers (so the scopes are not for the compiler, or war plugins, etc). This is required
 * so that the script writers can get the dependencies they need regardless of the Maven scopes (still want to use
 * the Maven scopes for everything else Maven, but for GWT-Maven we need to access deps differently - directly at
 * times).//  w w w .j  a v a2 s . c  o  m
 * 
 * @param project The maven project the Mojo is running for
 * @param scope
 * @param runtime
 * @param artifacts the project artifacts (all scopes)
 * @return file collection for classpath
 * @throws DependencyResolutionRequiredException
 */
@SuppressWarnings("unchecked")
public Collection<File> buildClasspathList(final MavenProject project, final String scope, GwtRuntime runtime,
        Set<Artifact> artifacts) throws MojoExecutionException {
    getLogger().info("establishing classpath list (scope = " + scope + ")");

    Set<File> items = new LinkedHashSet<File>();

    // Note : Don't call addSourceWithActiveProject as a GWT dependency MUST be a valid GWT library module :
    // * include java sources in the JAR as resources
    // * define a gwt.xml module file to declare the required inherits
    // addSourceWithActiveProject would make some java sources available to GWT compiler that should not be accessible in
    // a non-reactor build, making the build less deterministic and encouraging bad design.

    addSources(items, project.getCompileSourceRoots());
    addResources(items, project.getResources());
    items.add(new File(project.getBuild().getOutputDirectory()));

    // Use our own ClasspathElements fitering, as for RUNTIME we need to include PROVIDED artifacts,
    // that is not the default Maven policy, as RUNTIME is used here to build the GWTShell execution classpath

    if (scope.equals(SCOPE_TEST)) {
        addSources(items, project.getTestCompileSourceRoots());
        addResources(items, project.getTestResources());
        items.add(new File(project.getBuild().getTestOutputDirectory()));

        // Add all project dependencies in classpath
        for (Artifact artifact : artifacts) {
            items.add(artifact.getFile());
        }
    } else if (scope.equals(SCOPE_COMPILE)) {
        // Add all project dependencies in classpath
        getLogger().debug("candidate artifacts : " + artifacts.size());
        for (Artifact artifact : artifacts) {
            String artifactScope = artifact.getScope();
            if (SCOPE_COMPILE.equals(artifactScope) || SCOPE_PROVIDED.equals(artifactScope)
                    || SCOPE_SYSTEM.equals(artifactScope)) {
                items.add(artifact.getFile());
            }
        }
    } else if (scope.equals(SCOPE_RUNTIME)) {
        // Add all dependencies BUT "TEST" as we need PROVIDED ones to setup the execution
        // GWTShell that is NOT a full JEE server
        for (Artifact artifact : artifacts) {
            getLogger().debug("candidate artifact : " + artifact);
            if (!artifact.getScope().equals(SCOPE_TEST) && artifact.getArtifactHandler().isAddedToClasspath()) {
                items.add(artifact.getFile());
            }
        }
    } else {
        throw new IllegalArgumentException("unsupported scope " + scope);
    }

    if (runtime != null) {
        items.add(runtime.getGwtDevJar());
    }

    getLogger().debug("GWT SDK execution classpath :");
    for (File f : items) {
        getLogger().debug("   " + f.getAbsolutePath());
    }

    return items;
}

From source file:org.codehaus.mojo.gwt.shell.ClasspathBuilder.java

License:Apache License

/**
 * Get resources for specific scope.//from   ww w .j a v a 2 s  .  c o  m
 *
 * @param project
 * @param scope
 * @return
 */
@SuppressWarnings("unchecked")
private List<Resource> getResources(final MavenProject project, final String scope) {
    if (SCOPE_COMPILE.equals(scope) || SCOPE_RUNTIME.equals(scope)) {
        return project.getResources();
    } else if (SCOPE_TEST.equals(scope)) {
        List<Resource> resources = new ArrayList<Resource>();
        resources.addAll(project.getTestResources());
        resources.addAll(project.getResources());
        return resources;
    } else {
        throw new RuntimeException("Not allowed scope " + scope);
    }
}

From source file:org.cruxframework.crux.plugin.maven.ClasspathBuilder.java

License:Apache License

/**
 * Build classpath list using either gwtHome (if present) or using *project* dependencies. Note that this is ONLY used for the
 * script/cmd writers (so the scopes are not for the compiler, or war plugins, etc). This is required so that the script writers can get
 * the dependencies they need regardless of the Maven scopes (still want to use the Maven scopes for everything else Maven, but for
 * GWT-Maven we need to access deps differently - directly at times).
 *
 * @param project The maven project the Mojo is running for
 * @param artifacts the project artifacts (all scopes)
 * @param scope artifact scope to use//from  ww w.  j a va 2 s  .  c o m
 * @param isGenerator whether to use processed resources and compiled classes (false), or raw resources (true).
 * @return file collection for classpath
 * @throws MojoExecutionException
 */
public Collection<File> buildClasspathList(final MavenProject project, final String scope,
        Set<Artifact> artifacts, boolean isGenerator, boolean addSources) throws ClasspathBuilderException {
    getLogger().debug("establishing classpath list (scope = " + scope + ")");

    Set<File> items = new LinkedHashSet<File>();

    // Note : Don't call addSourceWithActiveProject as a GWT dependency MUST be a valid GWT library module :
    // * include java sources in the JAR as resources
    // * define a gwt.xml module file to declare the required inherits
    // addSourceWithActiveProject would make some java sources available to GWT compiler that should not be accessible in
    // a non-reactor build, making the build less deterministic and encouraging bad design.

    if (!isGenerator) {
        items.add(new File(project.getBuild().getOutputDirectory()));
    }
    if (addSources) {
        addSources(items, project.getCompileSourceRoots());
        if (isGenerator) {
            addResources(items, project.getResources());
        }
    }
    // Use our own ClasspathElements fitering, as for RUNTIME we need to include PROVIDED artifacts,
    // that is not the default Maven policy, as RUNTIME is used here to build the GWTShell execution classpath

    if (scope.equals(SCOPE_TEST)) {
        addSources(items, project.getTestCompileSourceRoots());
        addResources(items, project.getTestResources());
        items.add(new File(project.getBuild().getTestOutputDirectory()));

        // Add all project dependencies in classpath
        for (Artifact artifact : artifacts) {
            items.add(artifact.getFile());
        }
    } else if (scope.equals(SCOPE_COMPILE)) {
        // Add all project dependencies in classpath
        getLogger().debug("candidate artifacts : " + artifacts.size());
        for (Artifact artifact : artifacts) {
            String artifactScope = artifact.getScope();
            if (SCOPE_COMPILE.equals(artifactScope) || SCOPE_PROVIDED.equals(artifactScope)
                    || SCOPE_SYSTEM.equals(artifactScope)) {
                items.add(artifact.getFile());
            }
        }
    } else if (scope.equals(SCOPE_RUNTIME)) {
        // Add all dependencies BUT "TEST" as we need PROVIDED ones to setup the execution
        // GWTShell that is NOT a full JEE server
        for (Artifact artifact : artifacts) {
            getLogger().debug("candidate artifact : " + artifact);
            if (!artifact.getScope().equals(SCOPE_TEST) && artifact.getArtifactHandler().isAddedToClasspath()) {
                items.add(artifact.getFile());
            }
        }
    } else {
        throw new ClasspathBuilderException("unsupported scope " + scope);
    }
    return items;
}

From source file:org.eclipse.m2e.core.internal.embedder.MavenProjectMutableState.java

License:Open Source License

public static MavenProjectMutableState takeSnapshot(MavenProject project) {
    MavenProjectMutableState snapshot = new MavenProjectMutableState();

    if (project.getContextValue(CTX_SNAPSHOT) == null) {
        snapshot.compileSourceRoots = new ArrayList<String>(project.getCompileSourceRoots());
        snapshot.testCompileSourceRoots = new ArrayList<String>(project.getTestCompileSourceRoots());
        snapshot.resources = new ArrayList<Resource>(project.getResources());
        snapshot.testResources = new ArrayList<Resource>(project.getTestResources());

        snapshot.properties = new Properties();
        snapshot.properties.putAll(project.getProperties());

        project.setContextValue(CTX_SNAPSHOT, Boolean.TRUE);
        snapshot.nested = false;//  w  ww  .ja  v a  2s .c  om
    }

    return snapshot;
}