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

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

Introduction

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

Prototype

public List<String> getCompileClasspathElements() throws DependencyResolutionRequiredException 

Source Link

Usage

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

License:Apache License

/**
 * got from//from  w ww. j  av  a2 s.  com
 * 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.sun.jersey.wadl.AbstractMojoProjectClasspathSupport.java

License:Open Source License

/**
 * Create a list of classpath elements including declared build dependencies, the build
 * output directory and additionally configured dependencies.
 * @param mavenProject//ww w  .  ja v  a2s  .  c o  m
 * @param additionalDependencies
 * @return a list of classpath elements
 * @throws DependencyResolutionRequiredException
 * @throws ArtifactResolutionException
 * @throws ArtifactNotFoundException
 * @author Martin Grotzke
 */
protected List<String> getClasspathElements(final MavenProject mavenProject,
        final List<com.sun.jersey.wadl.Dependency> additionalDependencies)
        throws DependencyResolutionRequiredException, ArtifactResolutionException, ArtifactNotFoundException {

    final List<String> paths = new ArrayList<String>();

    /* Add maven compile classpath elements
     */
    @SuppressWarnings("unchecked")
    final List<String> compileClasspathElements = mavenProject.getCompileClasspathElements();
    paths.addAll(compileClasspathElements);

    /* Add build dependencies as classpath elements
     */
    @SuppressWarnings("unchecked")
    final Collection<Dependency> dependencies = mavenProject.getDependencies();
    if (dependencies != null) {
        for (Dependency dependency : dependencies) {
            if (dependency.getSystemPath() != null) {
                getLog().debug("Adding dependency with systemPath " + dependency.getSystemPath());
                paths.add(dependency.getSystemPath());
            } else {
                final Artifact artifact = artifactFactory.createArtifactWithClassifier(dependency.getGroupId(),
                        dependency.getArtifactId(), dependency.getVersion(), dependency.getType(),
                        dependency.getClassifier());
                resolver.resolve(artifact, remoteRepositories, localRepository);
                getLog().debug("Adding artifact " + artifact.getFile().getPath());
                paths.add(artifact.getFile().getPath());
            }
        }
    }

    /* Add additional dependencies
     */
    if (additionalDependencies != null) {
        for (com.sun.jersey.wadl.Dependency dependency : additionalDependencies) {
            if (dependency.getSystemPath() != null) {
                getLog().debug("Adding additional dependency with systemPath " + dependency.getSystemPath());
                paths.add(dependency.getSystemPath());
            } else {
                final Artifact artifact = artifactFactory.createArtifactWithClassifier(dependency.getGroupId(),
                        dependency.getArtifactId(), dependency.getVersion(), "jar", null);
                resolver.resolve(artifact, remoteRepositories, localRepository);
                getLog().debug("Adding additional artifact " + artifact.getFile().getPath());
                paths.add(artifact.getFile().getPath());
            }
        }
    }

    return paths;
}

From source file:com.zuppelli.living.docs.DiagramMojo.java

License:Apache License

private static List<String> compileClassPathElements(MavenProject project)
        throws DependencyResolutionRequiredException {
    return new ArrayList<String>(project.getCompileClasspathElements());
}

From source file:de.jpdigital.maven.plugins.hibernate4ddl.EntityFinder.java

License:Open Source License

/**
 * Creates an {@code EntityFinder} for the provided package.
 *
 * @param project     The Maven project in which the calling Mojo is
 *                    executed. Can be {@code null}.
 * @param log         An Maven log object for creating output.
 * @param packageName The name of the package in the class should look for
 *                    entities.//from   w w w . ja  v a  2s  . co  m
 *
 * @return An {@code EntityFinder} instance.
 *
 * @throws MojoFailureException If the {@link Reflections} instance needed
 *                              by the {@code EntityFinder} can't be
 *                              created.
 */
public static EntityFinder forPackage(final MavenProject project, final Log log, final String packageName)
        throws MojoFailureException {
    final Reflections reflections;
    if (project == null) {
        reflections = new Reflections(ClasspathHelper.forPackage(packageName));
    } else {
        final List<String> classPathElems;
        try {
            classPathElems = project.getCompileClasspathElements();
        } catch (DependencyResolutionRequiredException ex) {
            throw new MojoFailureException("Failed to resolve project classpath.", ex);
        }
        final List<URL> classPathUrls = new ArrayList<>();
        for (final String classPathElem : classPathElems) {
            log.info(String.format("Adding classpath elemement '%s'...", classPathElem));
            classPathUrls.add(classPathElemToUrl(classPathElem));
        }

        log.info("Classpath URLs:");
        for (final URL url : classPathUrls) {
            log.info(String.format("\t%s", url.toString()));
        }

        //Here we have to do some classloader magic to ensure that the Reflections instance
        //uses the correct class loader. Which is the one which has access to the compiled 
        //classes
        final ClassLoader classLoader = AccessController.doPrivileged(new ClassLoaderCreator(classPathUrls));

        reflections = new Reflections(ClasspathHelper.forPackage(packageName, classLoader));

    }

    return new EntityFinder(reflections);
}

From source file:de.jpdigital.maven.plugins.hibernate5ddl.EntityFinder.java

License:Open Source License

/**
 * Creates an {@code EntityFinder} for the provided package.
 *
 * @param project     The Maven project in which the calling Mojo is
 *                    executed. Can be {@code null}.
 * @param log         An Maven log object for creating output.
 * @param packageName The name of the package in the class should look for
 *                    entities./*from w w w  .  java  2  s  .  c  om*/
 *
 * @return An {@code EntityFinder} instance.
 *
 * @throws MojoFailureException If the {@link Reflections} instance needed
 *                              by the {@code EntityFinder} can't be
 *                              created.
 */
@SuppressWarnings("unchecked")
public static EntityFinder forPackage(final MavenProject project, final Log log, final String packageName)
        throws MojoFailureException {
    final Reflections reflections;
    if (project == null) {
        reflections = new Reflections(ClasspathHelper.forPackage(packageName));
    } else {
        final List<String> classPathElems;
        try {
            classPathElems = project.getCompileClasspathElements();
        } catch (DependencyResolutionRequiredException ex) {
            throw new MojoFailureException("Failed to resolve project classpath.", ex);
        }
        final List<URL> classPathUrls = new ArrayList<>();
        for (final String classPathElem : classPathElems) {
            log.info(String.format("Adding classpath elemement '%s'...", classPathElem));
            classPathUrls.add(classPathElemToUrl(classPathElem));
        }

        log.info("Classpath URLs:");
        for (final URL url : classPathUrls) {
            log.info(String.format("\t%s", url.toString()));
        }

        //Here we have to do some classloader magic to ensure that the Reflections instance
        //uses the correct class loader. Which is the one which has access to the compiled 
        //classes
        final ClassLoader classLoader = AccessController.doPrivileged(new ClassLoaderCreator(classPathUrls));

        reflections = new Reflections(ClasspathHelper.forPackage(packageName, classLoader));

    }

    return new EntityFinder(reflections);
}

From source file:guru.nidi.maven.tools.backport7to6.Backport7to6ArtifactMojo.java

License:Apache License

public void execute() throws MojoExecutionException, MojoFailureException {
    try {/*  w  ww  .j  av a  2 s. c om*/
        final File dir = createTempDir();
        final Artifact artifact = resolveAndUnzipArtifact(dir);
        final MavenProject project = createMavenProject(dir, artifact);
        MavenUtil.extendPluginClasspath(project.getCompileClasspathElements());
        if (new Backporter7to6(getChecker(project), getLog()).backportFiles(dir, dir.getAbsolutePath())) {
            IoUtil.zip(dir, targetFile(artifact.getFile()));
        } else {
            getLog().info("No conversion needed.");
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Could not backport", e);
    }
}

From source file:io.doov.gen.utils.ClassLoaderUtils.java

License:Apache License

public static URLClassLoader getUrlClassLoader(MavenProject project) throws MojoFailureException {
    try {//ww w  .j  a v a 2s .c  o m
        // collect compile classpath classes
        URL[] urls = project.getCompileClasspathElements().stream().map(File::new).filter(File::exists)
                .map(new FileToUrl()).toArray(URL[]::new);

        return new URLClassLoader(urls, currentThread().getContextClassLoader());
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}

From source file:io.fabric8.maven.core.util.ClassUtil.java

License:Apache License

public static URLClassLoader createProjectClassLoader(final MavenProject project, Logger log) {

    try {//from   w  w w. j  a v a 2  s  .c  om

        List<URL> compileJars = new ArrayList<>();

        for (String element : project.getCompileClasspathElements()) {
            compileJars.add(new File(element).toURI().toURL());
        }

        return new URLClassLoader(compileJars.toArray(new URL[compileJars.size()]),
                PluginServiceFactory.class.getClassLoader());

    } catch (Exception e) {
        log.warn("Instructed to use project classpath, but cannot. Continuing build if we can: ", e);
    }

    // return an empty CL .. don't want to have to deal with NULL later
    // if somehow we incorrectly call this method
    return new URLClassLoader(new URL[] {});
}

From source file:io.fabric8.maven.core.util.MavenUtil.java

License:Apache License

public static URLClassLoader getCompileClassLoader(MavenProject project) {
    try {/* www .  ja  v  a2s  .  c  o m*/
        List<String> classpathElements = project.getCompileClasspathElements();
        return createClassLoader(classpathElements, project.getBuild().getOutputDirectory());
    } catch (DependencyResolutionRequiredException e) {
        throw new IllegalArgumentException("Cannot resolve artifact from compile classpath", e);
    }
}

From source file:io.sarl.maven.compiler.AbstractSarlBatchCompilerMojo.java

License:Apache License

/** Replies the current classpath.
 *
 * @return the current classpath.//from  w w  w. j ava 2s  .  c  o m
 * @throws MojoExecutionException on failure.
 */
protected List<File> getClassPath() throws MojoExecutionException {
    final Set<String> classPath = new LinkedHashSet<>();
    final MavenProject project = getProject();
    classPath.add(project.getBuild().getSourceDirectory());
    try {
        classPath.addAll(project.getCompileClasspathElements());
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
    for (final Artifact dep : project.getArtifacts()) {
        classPath.add(dep.getFile().getAbsolutePath());
    }
    classPath.remove(project.getBuild().getOutputDirectory());
    final List<File> files = new ArrayList<>();
    for (final String filename : classPath) {
        final File file = new File(filename);
        if (file.exists()) {
            files.add(file);
        } else {
            getLog().warn(Locale.getString(AbstractSarlBatchCompilerMojo.class, "ERROR_3", filename)); //$NON-NLS-1$
        }
    }
    return files;
}