List of usage examples for org.apache.maven.project MavenProject getTestClasspathElements
public List<String> getTestClasspathElements() throws DependencyResolutionRequiredException
From source file:br.com.gumga.maven.plugins.gumgag.Util.java
public static ClassLoader getClassLoader(MavenProject project) { ClassLoader aRetornar = null; try {//from ww w.ja va 2 s.c o m List elementos = new ArrayList(); elementos.addAll(project.getRuntimeClasspathElements()); elementos.addAll(project.getTestClasspathElements()); URL[] runtimeUrls = new URL[elementos.size()]; for (int i = 0; i < elementos.size(); i++) { String element = (String) elementos.get(i); runtimeUrls[i] = new File(element).toURI().toURL(); } aRetornar = new URLClassLoader(runtimeUrls, Thread.currentThread().getContextClassLoader()); } catch (Exception ex) { ex.printStackTrace(); } return aRetornar; }
From source file:com.agilejava.docbkx.maven.AbstractTransformerMojo.java
License:Apache License
/** * Configures and executes the given ant tasks, mainly preprocess and postprocess defined in the pom configuration. * * @param antTasks The tasks to execute//from ww w .j a v a 2s .c o m * @param mavenProject The current maven project * @throws MojoExecutionException If something wrong occurs while executing the ant tasks. */ protected void executeTasks(Target antTasks, MavenProject mavenProject) throws MojoExecutionException { try { ExpressionEvaluator exprEvaluator = (ExpressionEvaluator) antTasks.getProject() .getReference("maven.expressionEvaluator"); Project antProject = antTasks.getProject(); PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(antProject); propertyHelper.setNext(new AntPropertyHelper(exprEvaluator, getLog())); DefaultLogger antLogger = new DefaultLogger(); antLogger.setOutputPrintStream(System.out); antLogger.setErrorPrintStream(System.err); antLogger.setMessageOutputLevel(2); antProject.addBuildListener(antLogger); antProject.setBaseDir(mavenProject.getBasedir()); Path p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getArtifacts().iterator(), File.pathSeparator)); antProject.addReference("maven.dependency.classpath", p); p = new Path(antProject); p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator)); 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); List artifacts = getArtifacts(); List list = new ArrayList(artifacts.size()); File file; for (Iterator i = artifacts.iterator(); i.hasNext(); list.add(file.getPath())) { Artifact a = (Artifact) i.next(); file = a.getFile(); if (file == null) throw new DependencyResolutionRequiredException(a); } p = new Path(antProject); p.setPath(StringUtils.join(list.iterator(), File.pathSeparator)); antProject.addReference("maven.plugin.classpath", p); getLog().info("Executing tasks"); antTasks.execute(); getLog().info("Executed tasks"); } catch (Exception e) { throw new MojoExecutionException("Error executing ant tasks", e); } }
From source file:com.datacoper.maven.util.ClassLoaderUtil.java
@SuppressWarnings("unchecked") public static ClassLoader loadClassLoader(MavenProject project) { try {/* w ww. j a v a 2s .co m*/ List<String> elements = CollectionsUtil.concat(project.getRuntimeClasspathElements(), project.getTestClasspathElements()); return initializeClassLoader(elements); } catch (DependencyResolutionRequiredException | MalformedURLException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } }
From source file:com.edugility.jpa.maven.plugin.ListEntityClassnamesMojo.java
License:Open Source License
/** * Returns a {@link Set} of {@link URL}s that represents the test * classpath.// w w w . java 2 s . c o m * * <p>This uses the {@linkplain #getProject() associated * <tt>MavenProject</tt>} to {@linkplain * MavenProject#getTestClasspathElements() supply the information}. * If that {@link MavenProject} is {@code null}, then an {@linkplain * Collection#isEmpty() empty} {@linkplain * Collections#unmodifiableSet(Set) unmodifiable <tt>Set</tt>} is * returned.</p> * * <p>{@link String}-to-{@link URL} conversion is accomplished like * this:</p> * * <ul> * * <li>The {@link MavenProject#getTestClasspathElements()} method * returns an untyped {@link List}. There is no contractual * guarantee about the type of its contents. Each element is * therefore treated as an {@link Object}.</li> * * <li>If the element is non-{@code null}, then its {@link * Object#toString()} method is invoked. The resulting {@link * String} is used to {@linkplain File#File(String) construct a * <tt>File</tt>}.</li> * * <li>The resulting {@link File}'s {@link File#toURI()} method is * invoked and the {@linkplain URI result}'s {@link URI#toURL()} * method is invoked. The return value is added to the {@link Set} * that will be returned.</li> * * </ul> * * <p>This method never returns {@code null}.</p> * * @return a {@link Set} of {@link URL}s representing the test * classpath, never {@code null}. The {@link Set}'s iteration order * is guaranteed to be equal to that of the iteration order of the * return value of the {@link * MavenProject#getTestClasspathElements()} method. * * @exception DependencyResolutionRequiredException if the {@link * MavenProject#getTestClasspathElements()} method throws a {@link * DependencyResolutionRequiredException} */ private final Set<URL> getTestClasspathURLs() throws DependencyResolutionRequiredException { final Set<URL> urls; final Log log = this.getLog(); assert log != null; final MavenProject project = this.getProject(); final List<?> classpathElements; if (project == null) { classpathElements = null; } else { classpathElements = project.getTestClasspathElements(); } if (classpathElements == null || classpathElements.isEmpty()) { if (log.isWarnEnabled()) { log.warn(String .format("The test classpath contained no elements. Consequently no Entities were found.")); } urls = Collections.emptySet(); } else { final Set<URL> mutableUrls = new LinkedHashSet<URL>(classpathElements.size()); for (final Object o : classpathElements) { if (o != null) { final File file = new File(o.toString()); if (file.canRead()) { try { mutableUrls.add(file.toURI().toURL()); } catch (final MalformedURLException wontHappen) { throw (InternalError) new InternalError(String.format( "While attempting to convert a file, %s, into a URL, a MalformedURLException was encountered.", file)).initCause(wontHappen); } } else if (log.isWarnEnabled()) { log.warn(String.format("The test classpath element %s could not be read.", file)); } } } if (mutableUrls.isEmpty()) { urls = Collections.emptySet(); } else { urls = Collections.unmodifiableSet(mutableUrls); } } if (log.isWarnEnabled() && urls.isEmpty()) { log.warn(String.format("No URLs were found from the test classpath (%s).", classpathElements)); } return urls; }
From source file:com.sixdegreeshq.sitenav.GeneratorMojo.java
License:Apache License
/** * got from/*w w w .java2 s . co 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:hu.javaportal.maven.plugin.PersistenceGeneratorMojo.java
License:Apache License
public void execute2() throws MojoFailureException { final MavenProject project = this.getProject(); final List<?> classpathElements; getLog().debug("WTFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); if (project == null) { classpathElements = null;/*w ww. jav a 2 s . c o m*/ } else { try { classpathElements = project.getTestClasspathElements(); getLog().debug("Found elements" + classpathElements.size()); for (Object e : classpathElements) { getLog().debug(e.toString()); } } catch (DependencyResolutionRequiredException e) { getLog().error(e); } } }
From source file:io.fabric8.maven.core.util.MavenUtil.java
License:Apache License
public static URLClassLoader getTestClassLoader(MavenProject project) { try {// ww w .j ava2s . c o m List<String> classpathElements = project.getTestClasspathElements(); return createClassLoader(classpathElements, project.getBuild().getTestOutputDirectory()); } catch (DependencyResolutionRequiredException e) { throw new IllegalArgumentException("Cannot resolve artifact from test classpath", e); } }
From source file:org.codehaus.mojo.webtest.components.AntExecutor.java
License:Apache License
/** * Run an ANT script within the JVM../*w w w . j a va 2 s . c o m*/ * * @param antFile the ANT scripts to be executed * @param userProperties the properties to be set for the ANT script * @param mavenProject the current maven project * @param artifacts the list of dependencies * @param target the ANT target to be executed * @throws DependencyResolutionRequiredException * not dependencies were resolved * @throws BuildException the build failed */ public AntExecutor(File antFile, Properties userProperties, MavenProject mavenProject, List artifacts, String target) throws BuildException, DependencyResolutionRequiredException { File antBaseDir = antFile.getParentFile(); Project antProject = new Project(); antProject.init(); antProject.addBuildListener(this.createLogger()); antProject.setBaseDir(antBaseDir); ProjectHelper2.configureProject(antProject, antFile); // ProjectHelper2 projectHelper = new ProjectHelper2(); // projectHelper.parse( antProject, antFile ); Enumeration propertyKeys = userProperties.keys(); while (propertyKeys.hasMoreElements()) { String key = (String) propertyKeys.nextElement(); String value = userProperties.getProperty(key); antProject.setUserProperty(key, value); } // NOTE: from maven-antrun-plugin 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(artifacts, antProject)); antProject.executeTarget(target); }
From source file:org.evosuite.maven.util.ProjectUtils.java
License:Open Source License
/** * Get JUnit elements (i.e., classes under /target/test-classes) and compiled * elements (i.e., classes under /target/classes) * //from ww w. ja va 2s. c om * @param project * @return */ public static List<String> getTestClasspathElements(MavenProject project) { List<String> testClassPath = new ArrayList<String>(); try { project.getTestClasspathElements().stream() // we only target what has been compiled to a folder .filter(element -> !element.endsWith(".jar")).filter(element -> new File(element).exists()) .forEach(element -> testClassPath.add(element)); } catch (DependencyResolutionRequiredException e) { e.printStackTrace(); } return testClassPath; }
From source file:org.fourthline.lemma.maven.LemmaMojo.java
License:Open Source License
public XHTMLTemplateJavadocPipeline createPipeline(List<File> sourceDirectories, List<String> packageNames, MavenProject project) throws Exception { // Yep, the Javadoc tool has its own classpath, we abuse the javac system property to get it into the Gaftercode String javadocClasspath = null; try {//w ww . java 2 s. c o m List<String> classpathElements = (List<String>) project.getTestClasspathElements(); StringBuilder sb = new StringBuilder(); for (String classpathElement : classpathElements) { sb.append(classpathElement).append(File.pathSeparator); } if (sb.length() > 0) sb.deleteCharAt(sb.length() - 1); javadocClasspath = sb.toString(); if (javadocClasspath != null) { getLog().debug("Setting Javadoc classpath: " + javadocClasspath); System.setProperty("env.class.path", javadocClasspath); // The Javadoc code reads this env variable! } } catch (DependencyResolutionRequiredException ex) { throw new Exception("Can't get test-scope classpath: " + ex.toString(), ex); } // Hurray for more logging abstractions! Handler loggingAdapter = new Handler() { Formatter formatter = new Formatter() { @Override public String format(LogRecord logRecord) { return formatMessage(logRecord); } }; @Override public void publish(LogRecord logRecord) { if (logRecord.getLevel().equals(Level.SEVERE) && getLog().isErrorEnabled()) { getLog().error(formatter.format(logRecord)); } else if (logRecord.getLevel().equals(Level.WARNING) && getLog().isWarnEnabled()) { getLog().warn(formatter.format(logRecord)); } else if (logRecord.getLevel().equals(Level.INFO) && getLog().isInfoEnabled()) { getLog().info(formatter.format(logRecord)); } else if (getLog().isDebugEnabled()) { getLog().debug(formatter.format(logRecord)); } } @Override public void flush() { } @Override public void close() throws SecurityException { } }; loggingAdapter.setLevel(Level.ALL); LoggingUtil.resetRootHandler(loggingAdapter); LogManager.getLogManager().getLogger("").setLevel(Level.ALL); // Check the configuration for (File sourceDirectory : sourceDirectories) { if (!sourceDirectory.canRead()) { throw new Exception("Source directory not found or not readable: " + sourceDirectory); } if (!sourceDirectory.isDirectory()) { throw new Exception("Source directory is not a directory: " + sourceDirectory); } } if (packageNames.size() == 0) { for (File sourceDirectory : sourceDirectories) { // Default to all sub-directories in source directory File[] subdirs = sourceDirectory.listFiles(new FileFilter() { public boolean accept(File file) { return file.isDirectory() && file.getName().matches("[a-zA-Z_]+"); } }); for (File subdir : subdirs) { packageNames.add(subdir.getName()); } // Filter duplicates packageNames = new ArrayList(new LinkedHashSet(packageNames)); } } // Finally, do the work return new XHTMLTemplateJavadocPipeline(sourceDirectories, packageNames, true, processXRefs); }