List of usage examples for org.apache.maven.project MavenProject getCompileClasspathElements
public List<String> getCompileClasspathElements() throws DependencyResolutionRequiredException
From source file:org.evosuite.maven.util.ProjectUtils.java
License:Open Source License
/** * Get compile elements (i.e., classes under /target/classes) * /*from ww w . j a va2s .c o m*/ * @param project * @return */ public static List<String> getCompileClasspathElements(MavenProject project) { List<String> compileClassPath = new ArrayList<String>(); try { project.getCompileClasspathElements().stream() // we only target what has been compiled to a folder .filter(element -> !element.endsWith(".jar")).filter(element -> new File(element).exists()) .forEach(element -> compileClassPath.add(element)); } catch (DependencyResolutionRequiredException e) { e.printStackTrace(); } return compileClassPath; }
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 ww w . j av a 2 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> getCompileClasspathElements(Project proj) throws DependencyResolutionRequiredException { MavenProject mavenProj = MavenHelper.getOriginalMavenProject(proj); if (mavenProj == null) { return Collections.emptyList(); }//w ww. j ava 2s. c o m List<String> compileClasspath = mavenProj.getCompileClasspathElements(); if (compileClasspath == null || compileClasspath.isEmpty()) { KotlinLogger.INSTANCE.logInfo(proj.getProjectDirectory().getPath() + " compile classpath is empty"); } return compileClasspath; }
From source file:org.jfrog.maven.annomojo.extractor.AnnoMojoDescriptorExtractor.java
License:Apache License
@SuppressWarnings({ "unchecked" }) public List<MojoDescriptor> execute(MavenProject project, PluginDescriptor pluginDescriptor) throws InvalidPluginDescriptorException { List<String> sourceRoots = project.getCompileSourceRoots(); Set<String> sourcePathElements = new HashSet<String>(); String srcRoot = null;/*from w w w. j a v a 2s . c om*/ try { for (String sourceRoot : sourceRoots) { srcRoot = sourceRoot; List<File> files = FileUtils.getFiles(new File(srcRoot), "**/*.java", null, true); for (File file : files) { String path = file.getPath(); sourcePathElements.add(path); } } } catch (Exception e) { throw new InvalidPluginDescriptorException("Failed to get source files from " + srcRoot, e); } List<String> argsList = new ArrayList<String>(); argsList.add("-nocompile"); argsList.add("-cp"); StringBuilder cp = new StringBuilder(); //Add the compile classpath List<String> compileClasspathElements; try { compileClasspathElements = project.getCompileClasspathElements(); } catch (DependencyResolutionRequiredException e) { throw new InvalidPluginDescriptorException("Failed to get compileClasspathElements.", e); } for (String ccpe : compileClasspathElements) { appendToPath(cp, ccpe); } //Add the current CL classptah URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs(); for (URL url : urls) { String path; try { path = url.getPath(); } catch (Exception e) { throw new InvalidPluginDescriptorException("Failed to get classpath files from " + url, e); } appendToPath(cp, path); } // Attempts to add dependencies to the classpath so that parameters inherited from abstract mojos in other // projects will be processed. Set s = project.getDependencyArtifacts(); if (s != null) { for (Object untypedArtifact : project.getDependencyArtifacts()) { if (untypedArtifact instanceof Artifact) { Artifact artifact = (Artifact) untypedArtifact; File artifactFile = artifact.getFile(); if (artifactFile != null) { appendToPath(cp, artifactFile.getAbsolutePath()); } } } } String classpath = cp.toString(); debug("cl=" + classpath); argsList.add(classpath); argsList.addAll(sourcePathElements); String[] args = argsList.toArray(new String[argsList.size()]); List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>(); MojoDescriptorTls.setDescriptors(descriptors); try { Main.process(new MojoApf(pluginDescriptor), new PrintWriter(System.out), args); } catch (Throwable t) { //TODO: [by yl] This is never caught - apt swallows the exception. //Use the TLS to hold thrown exception throw new InvalidPluginDescriptorException("Failed to extract plugin descriptor.", t); } return MojoDescriptorTls.getDescriptors(); }
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(); }// w w w . j a v a2 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. ja va2 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.jvnet.sorcerer.maven_plugin.SorcererReport.java
License:Apache License
protected List<String> getClasspathElements() throws DependencyResolutionRequiredException { if (!isAggregator()) return project.getCompileClasspathElements(); List<String> r = new ArrayList<String>(); for (MavenProject p : reactorProjects) r.addAll(p.getCompileClasspathElements()); return r;//from w w w . ja va2 s. co m }
From source file:org.levigo.m2e.assertj.internal.AssertJBuildParticipant.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* w w w . jav a 2s .co m*/ public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception { final BuildContext buildContext = getBuildContext(); MavenProject mavenProject = getMavenProjectFacade().getMavenProject(monitor); List<String> classpathElements = new ArrayList<String>(mavenProject.getCompileClasspathElements()); classpathElements.addAll(mavenProject.getTestClasspathElements()); ClassFileMatcher deltaScanner = new ClassFileMatcher(// (List<String>) getMojoParameterValue("packages", List.class, monitor), (List<String>) getMojoParameterValue("classes", List.class, monitor), (List<String>) getMojoParameterValue("includes", List.class, monitor), (List<String>) getMojoParameterValue("excludes", List.class, monitor)); boolean foundDelta = false; // Check for POM change Scanner pomScanner = buildContext.newScanner(mavenProject.getFile()); pomScanner.scan(); if (pomScanner.getIncludedFiles().length > 0) { log.info("###################### Found pom change"); cleanTargetFolder(monitor); foundDelta = true; } // Check for resource change if (!foundDelta) for (String classpathElement : classpathElements) { File f = new File(classpathElement); if (f.isDirectory()) { String[] deletions = buildContext.newDeleteScanner(f).getIncludedFiles(); if (null != deletions && deletions.length > 0) { log.info("###################### Found deletion in " + f); cleanTargetFolder(monitor); foundDelta = true; break; } else { Scanner ds = buildContext.newScanner(f); ds.scan(); String[] includedFiles = ds.getIncludedFiles(); if (includedFiles != null) for (String file : includedFiles) { foundDelta |= deltaScanner.matches(file); log.info("###################### Found matching class file " + file + ": " + foundDelta); break; } } log.info("###################### Check for delta in " + f + ": " + foundDelta); } if (foundDelta) break; } if (!foundDelta) { log.info("No changes"); return null; } log.info("Running template generation"); // execute mojo Set<IProject> result = super.build(kind, monitor); // tell m2e builder to refresh generated files final File generated = getTargetDir(monitor); if (generated != null) { buildContext.refresh(generated); /* * For some weird reason the java build triggered by buildContext.refresh(generated) above * does not (yet) see the changed class file (although we detected the changed class file * result). This causes compile errors upon added/removed/updated fields and properties. We * schedule another workspace refresh to correct this situation. */ new Job("Refresh generated assertions") { @Override protected IStatus run(IProgressMonitor monitor) { log.info("Refreshing generated assertions"); IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); try { IJobManager jobManager = Job.getJobManager(); jobManager.join(ResourcesPlugin.FAMILY_MANUAL_BUILD, monitor); jobManager.join(ResourcesPlugin.FAMILY_AUTO_BUILD, monitor); myWorkspaceRoot.getFolder(Path.fromOSString(generated.getAbsoluteFile().getCanonicalPath())) .refreshLocal(IResource.DEPTH_INFINITE, monitor); } catch (Exception e) { log.error("Failed to refresh the generated assertions", e); } return Status.OK_STATUS; } }.schedule(); } return result; }
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);/*w ww .ja va 2s. c o m*/ 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.liquigraph.maven.ProjectClassLoader.java
License:Apache License
@SuppressWarnings("unchecked") private static List<String> compileClassPathElements(MavenProject project) throws DependencyResolutionRequiredException { return newArrayList(project.getCompileClasspathElements()); }