List of usage examples for org.apache.maven.project MavenProject getRuntimeClasspathElements
public List<String> getRuntimeClasspathElements() throws DependencyResolutionRequiredException
From source file:org.evosuite.maven.util.ProjectUtils.java
License:Open Source License
/** * Get runtime elements/* www. j ava 2 s .c om*/ * * @param project * @return */ public static List<String> getRuntimeClasspathElements(MavenProject project) { List<String> runtimeClassPath = new ArrayList<String>(); try { project.getRuntimeClasspathElements().stream().filter(element -> new File(element).exists()) .forEach(element -> runtimeClassPath.add(element)); } catch (DependencyResolutionRequiredException e) { e.printStackTrace(); } return runtimeClassPath; }
From source file:org.forgerock.opendj.maven.doc.Utils.java
License:CDDL license
/** * Returns a ClassLoader including the project's runtime classpath elements. * This is useful when running a Java command from inside a Maven plugin. * * @param project The Maven project holding runtime classpath elements. * @param log A plugin log to use for debugging. * @return A ClassLoader including the project's runtime classpath elements. * @throws DependencyResolutionRequiredException Failed to access the runtime classpath * @throws MalformedURLException Failed to add an element to the classpath *///from w w w . j av a2 s . c o m static URLClassLoader getRuntimeClassLoader(MavenProject project, Log log) throws DependencyResolutionRequiredException, MalformedURLException { List<String> runtimeClasspathElements = project.getRuntimeClasspathElements(); Set<URL> runtimeUrls = new LinkedHashSet<>(); for (String element : runtimeClasspathElements) { runtimeUrls.add(new File(element).toURI().toURL()); } final URLClassLoader urlClassLoader = new URLClassLoader( runtimeUrls.toArray(new URL[runtimeClasspathElements.size()]), Thread.currentThread().getContextClassLoader()); debugClassPathElements(urlClassLoader, log); return urlClassLoader; }
From source file:org.javagems.core.maven.DebianMojo.java
License:Apache License
/** * @see org.apache.maven.plugin.Mojo#execute() * @since modified by <a href="mailto:christophe@keyade.com">Christophe Cassagnabere</a> on lines 211-227 *///from w w w.j ava2 s . c o m public void execute() throws MojoExecutionException { if (skip) { getLog().info("Skipping Antrun execution"); return; } MavenProject mavenProject = getMavenProject(); if (target == null && buildFile == null) { getLog().info("No ant target defined - SKIPPED"); return; } if (target == null) { target = new XmlPlexusConfiguration("target"); } if (buildFile != null) { XmlPlexusConfiguration tg = new XmlPlexusConfiguration("target"); tg.setAttribute("name", targetName); XmlPlexusConfiguration ant = new XmlPlexusConfiguration("ant"); ant.setAttribute("antfile", buildFile); ant.addChild(tg); target.addChild(ant); } if (propertyPrefix == null) { propertyPrefix = ""; } try { Project antProject = new Project(); File antBuildFile = this.writeTargetToProjectFile(); ProjectHelper.configureProject(antProject, antBuildFile); antProject.init(); DefaultLogger antLogger = new DefaultLogger(); antLogger.setOutputPrintStream(System.out); antLogger.setErrorPrintStream(System.err); if (getLog().isDebugEnabled()) { antLogger.setMessageOutputLevel(Project.MSG_DEBUG); } else if (getLog().isInfoEnabled()) { antLogger.setMessageOutputLevel(Project.MSG_INFO); } else if (getLog().isWarnEnabled()) { antLogger.setMessageOutputLevel(Project.MSG_WARN); } else if (getLog().isErrorEnabled()) { antLogger.setMessageOutputLevel(Project.MSG_ERR); } else { antLogger.setMessageOutputLevel(Project.MSG_VERBOSE); } antProject.addBuildListener(antLogger); antProject.setBaseDir(mavenProject.getBasedir()); Path p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator)); /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */ antProject.addReference("maven.dependency.classpath", p); antProject.addReference("maven.compile.classpath", p); p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator)); antProject.addReference("maven.runtime.classpath", p); p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getTestClasspathElements().iterator(), File.pathSeparator)); antProject.addReference("maven.test.classpath", p); /* set maven.plugin.classpath with plugin dependencies */ antProject.addReference("maven.plugin.classpath", getPathFromArtifacts(pluginArtifacts, antProject)); antProject.addReference(DEFAULT_MAVEN_PROJECT_REFID, getMavenProject()); antProject.addReference(DEFAULT_MAVEN_PROJECT_HELPER_REFID, projectHelper); antProject.addReference("maven.local.repository", localRepository); initMavenTasks(antProject); // The ant project needs actual properties vs. using expression evaluator when calling an external build // file. copyProperties(mavenProject, antProject); if (getLog().isInfoEnabled()) { getLog().info("Executing tasks"); } antProject.executeTarget(antTargetName); if (getLog().isInfoEnabled()) { getLog().info("Executed tasks"); } copyProperties(antProject, mavenProject); } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("DependencyResolutionRequiredException: " + e.getMessage(), e); } catch (BuildException e) { StringBuffer sb = new StringBuffer(); sb.append("An Ant BuildException has occured: " + e.getMessage()); String fragment = findFragment(e); if (fragment != null) { sb.append("\n").append(fragment); } if (!failOnError) { getLog().info(sb.toString(), e); return; // do not register roots. } else { throw new MojoExecutionException(sb.toString(), e); } } catch (Throwable e) { throw new MojoExecutionException("Error executing ant tasks: " + e.getMessage(), e); } }
From source file:org.jetbrains.kotlin.projectsextensions.maven.classpath.MavenExtendedClassPath.java
License:Apache License
private List<String> getRuntimeClasspathElements(Project proj) throws DependencyResolutionRequiredException { MavenProject mavenProj = MavenHelper.getOriginalMavenProject(proj); if (mavenProj == null) { return Collections.emptyList(); }/* w w w . ja v a2 s . c om*/ List<String> runtimeClasspath = mavenProj.getRuntimeClasspathElements(); if (runtimeClasspath == null || runtimeClasspath.isEmpty()) { KotlinLogger.INSTANCE.logInfo(proj.getProjectDirectory().getPath() + " runtime classpath is empty"); } return runtimeClasspath; }
From source file:org.jszip.maven.RunMojo.java
License:Apache License
@SuppressWarnings("unchecked") private List<String> getClasspathElements(MavenProject project, String scope) throws DependencyResolutionRequiredException { if ("test".equals(scope)) { return project.getTestClasspathElements(); }/*from w w w . ja va 2 s. com*/ if ("compile".equals(scope)) { return project.getCompileClasspathElements(); } if ("runtime".equals(scope)) { return project.getRuntimeClasspathElements(); } return Collections.emptyList(); }
From source file:org.jvnet.maven.plugin.antrun.AbstractAntMojo.java
License:Apache License
/** * @param antTasks//from w w w. j av a 2 s . c o m * @param mavenProject * @throws MojoExecutionException */ protected void executeTasks(Target antTasks, MavenProject mavenProject, List pluginArtifacts) throws MojoExecutionException { if (antTasks == null) { getLog().info("No ant tasks defined - SKIPPED"); return; } try { //TODO refactor - place the manipulation of the expressionEvaluator into a separated class. ExpressionEvaluator exprEvaluator = (ExpressionEvaluator) antTasks.getProject() .getReference(AntTargetConverter.MAVEN_EXPRESSION_EVALUATOR_ID); Project antProject = antTasks.getProject(); PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(antProject); propertyHelper.setNext(new AntPropertyHelper(exprEvaluator, mavenProject.getArtifacts(), getLog())); DefaultLogger antLogger = new DefaultLogger(); antLogger.setOutputPrintStream(System.out); antLogger.setErrorPrintStream(System.err); antLogger.setMessageOutputLevel(getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO); antProject.addBuildListener(antLogger); antProject.setBaseDir(mavenProject.getBasedir()); Path p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator)); /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */ antProject.addReference("maven.dependency.classpath", p); antProject.addReference("maven.compile.classpath", p); p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator)); antProject.addReference("maven.runtime.classpath", p); p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getTestClasspathElements().iterator(), File.pathSeparator)); antProject.addReference("maven.test.classpath", p); /* set maven.plugin.classpath with plugin dependencies */ antProject.addReference("maven.plugin.classpath", getPathFromArtifacts(pluginArtifacts, antProject)); if (getLog().isInfoEnabled()) { getLog().info("Executing tasks"); } configureProject(antProject); antTasks.execute(); if (getLog().isInfoEnabled()) { getLog().info("Executed tasks"); } } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("DependencyResolutionRequiredException: " + e.getMessage(), e); } catch (BuildException e) { throw new MojoExecutionException("An Ant BuildException has occured: " + e.getMessage(), e); } catch (Exception e) { throw new MojoExecutionException("Error executing ant tasks: " + e.getMessage(), e); } }
From source file:org.lib4j.maven.mojo.MojoUtil.java
License:Open Source License
public static File[] getExecutionClasspash(final MojoExecution execution, final PluginDescriptor pluginDescriptor, final MavenProject project, final ArtifactRepository localRepository, final ArtifactHandler artifactHandler) throws DependencyResolutionRequiredException { final List<String> classpath = MojoUtil.getPluginDependencyClassPath(pluginDescriptor, localRepository, artifactHandler);//from ww w . j av a 2 s.com classpath.addAll(project.getCompileClasspathElements()); classpath.addAll(project.getRuntimeClasspathElements()); if (MojoUtil.isInTestPhase(execution)) { classpath.addAll(project.getTestClasspathElements()); classpath.addAll(MojoUtil.getProjectExecutionArtifactClassPath(project, localRepository)); } final File[] classpathFiles = new File[classpath.size()]; for (int i = 0; i < classpathFiles.length; i++) classpathFiles[i] = new File(classpath.get(i)); return classpathFiles; }
From source file:org.mule.tools.maven.plugin.module.analyze.ModuleDiscoverer.java
License:Open Source License
/** * Discovers all the Mule modules used as dependencies on the Maven project under analysis * * @param project project being analyzed. * @param analyzerLogger collects all the logging information generated during the project analysis * @param projectModuleName name of the module that corresponds to the project being analyzed * @return a list containing all the Mule modules that are dependencies of the analyzed project. * @throws ModuleApiAnalyzerException//w w w. j a v a 2 s .com */ public List<Module> discoverExternalModules(MavenProject project, AnalyzerLogger analyzerLogger, String projectModuleName) throws ModuleApiAnalyzerException { final List<Module> result = new LinkedList<>(); Set<URL> urls = new HashSet<>(); List<String> elements; try { elements = project.getRuntimeClasspathElements(); elements.addAll(project.getCompileClasspathElements()); for (String element : elements) { urls.add(new File(element).toURI().toURL()); } ClassLoader contextClassLoader = URLClassLoader.newInstance(urls.toArray(new URL[0]), currentThread().getContextClassLoader()); try { final Enumeration<URL> resources = contextClassLoader.getResources(MULE_MODULE_PROPERTIES_LOCATION); while (resources.hasMoreElements()) { final URL url = resources.nextElement(); Properties properties = loadProperties(url); // Skips project module properties String moduleName = (String) properties.get("module.name"); if (!moduleName.equals(projectModuleName)) { result.add(moduleFactory.create(analyzerLogger, moduleName, properties)); } } } catch (Exception e) { throw new ModuleApiAnalyzerException("Cannot read " + MULE_MODULE_PROPERTIES_LOCATION, e); } } catch (Exception e) { throw new ModuleApiAnalyzerException("Error getting project resources", e); } return result; }