List of usage examples for org.apache.maven.project MavenProject getRuntimeClasspathElements
public List<String> getRuntimeClasspathElements() throws DependencyResolutionRequiredException
From source file:br.com.gumga.maven.plugins.gumgag.Util.java
public static ClassLoader getClassLoader(MavenProject project) { ClassLoader aRetornar = null; try {/*from w w w. jav a2 s . c om*/ 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:ch.ifocusit.livingdoc.plugin.utils.ClassLoaderUtil.java
License:Apache License
public static ClassLoader getRuntimeClassLoader(MavenProject project) throws MojoExecutionException { try {/*from w w w . j ava 2 s.c o m*/ List<String> runtimeClasspathElements = project.getRuntimeClasspathElements(); List<String> compileClasspathElements = project.getCompileClasspathElements(); URL[] runtimeUrls = new URL[runtimeClasspathElements.size() + compileClasspathElements.size()]; for (int i = 0; i < runtimeClasspathElements.size(); i++) { String element = runtimeClasspathElements.get(i); runtimeUrls[i] = new File(element).toURI().toURL(); } int j = runtimeClasspathElements.size(); for (int i = 0; i < compileClasspathElements.size(); i++) { String element = compileClasspathElements.get(i); runtimeUrls[i + j] = new File(element).toURI().toURL(); } return new URLClassLoader(runtimeUrls, Thread.currentThread().getContextClassLoader()); } catch (Exception e) { throw new MojoExecutionException("Unable to load project runtime !", e); } }
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 w ww.ja v a 2 s. c om*/ * @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 {/*from w ww .j av a 2s . com*/ 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:de.jiac.micro.util.ReducedArchiver.java
License:Open Source License
/** * Return a pre-configured manifest/*from w w w. j ava 2 s . co m*/ * * @todo Add user attributes list and user groups list */ public Manifest getManifest(MavenProject project, ManifestConfiguration config) throws ManifestException, DependencyResolutionRequiredException { // Added basic entries Manifest m = new Manifest(); Manifest.Attribute buildAttr = new Manifest.Attribute("Built-By", System.getProperty("user.name")); m.addConfiguredAttribute(buildAttr); Manifest.Attribute createdAttr = new Manifest.Attribute("Created-By", "Apache Maven"); m.addConfiguredAttribute(createdAttr); if (config.getPackageName() != null) { Manifest.Attribute packageAttr = new Manifest.Attribute("Package", config.getPackageName()); m.addConfiguredAttribute(packageAttr); } Manifest.Attribute buildJdkAttr = new Manifest.Attribute("Build-Jdk", System.getProperty("java.version")); m.addConfiguredAttribute(buildJdkAttr); if (config.isAddClasspath()) { StringBuffer classpath = new StringBuffer(); List artifacts = project.getRuntimeClasspathElements(); String classpathPrefix = config.getClasspathPrefix(); for (Iterator iter = artifacts.iterator(); iter.hasNext();) { File f = new File((String) iter.next()); if (f.isFile()) { if (classpath.length() > 0) { classpath.append(" "); } classpath.append(classpathPrefix); classpath.append(f.getName()); } } if (classpath.length() > 0) { Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", classpath.toString()); m.addConfiguredAttribute(classpathAttr); } } String mainClass = config.getMainClass(); if (mainClass != null && !"".equals(mainClass)) { Manifest.Attribute mainClassAttr = new Manifest.Attribute("Main-Class", mainClass); m.addConfiguredAttribute(mainClassAttr); } return m; }
From source file:org.boretti.drools.integration.drools5.DroolsHelper.java
License:Open Source License
public static PackageBuilder getPackageBuilder(Log logger, MavenProject project) throws MojoExecutionException { if (logger.isDebugEnabled()) logger.debug("starting creation of package builder"); ClassLoader loader = DroolsHelper.class.getClassLoader(); List<?> classpathFiles = null; try {//from w w w .j a v a2 s .co m classpathFiles = project.getRuntimeClasspathElements(); } catch (Exception e) { throw new MojoExecutionException("Error during build " + e.getMessage(), e); } URL[] urls = new URL[classpathFiles.size()]; for (int i = 0; i < classpathFiles.size(); ++i) { try { urls[i] = new File((String) classpathFiles.get(i)).toURI().toURL(); } catch (MalformedURLException e) { throw new MojoExecutionException("Error during build " + e.getMessage(), e); } } URLClassLoader ucl = new URLClassLoader(urls, loader); PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); conf.setClassLoader(ucl); return new PackageBuilder(conf); }
From source file:org.codehaus.mojo.j2me.PreverifyMojo.java
License:Open Source License
private String getClassPath(MavenProject project) throws MojoExecutionException { try {/*www.j a v a 2 s.co m*/ // create buffer receiving full classpath StringBuffer classPath = new StringBuffer(); // loop through classpath elements List artifacts = project.getRuntimeClasspathElements(); for (int idx = 0; idx < artifacts.size(); idx++) { // get next dependency String classpathElement = (String) artifacts.get(idx); // add path seperator if necessary if (classPath.length() > 0) { classPath.append(File.pathSeparator); } // add path to dependency classPath.append(classpathElement); } log.debug("classpath: " + classPath); return classPath.toString(); } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("faild to resolve dependency", e); } }
From source file:org.codehaus.mojo.runtime.builder.MavenJarRuntimeBuilder.java
License:Open Source License
private void prepareRuntimeExecutionDependencies(MavenProject project) throws MavenRuntimeBuilderException { try {/* ww w . j a v a2 s . c o m*/ List classpathElements = project.getRuntimeClasspathElements(); for (Iterator i = classpathElements.iterator(); i.hasNext();) { String element = (String) i.next(); if (element.endsWith(".jar")) { FileUtils.copyFileToDirectory((String) i.next(), runtimeLibraryDirectory.getAbsolutePath()); } } } catch (Exception e) { throw new MavenRuntimeBuilderException("unable to prepare runtime execution dependencies", e); } }
From source file:org.codehaus.mojo.runtime.builder.MavenShellRuntimeBuilder.java
License:Open Source License
public void build(Runtime runtime, MavenProject project, File runtimeFile, File outputDirectory) throws MavenRuntimeBuilderException { try {//from w w w . j a v a 2s . c om initializeOutputDirectories(runtime, outputDirectory); // drop shell script String scriptName = runtimeExecutableDirectory + File.separator + runtime.getShell().getId() + ".sh"; IOUtil.copy(getClass().getResourceAsStream("/templates/runtime-sh.template"), new FileWriter(new File(scriptName))); // copy libs to script dir List classpathElements = project.getRuntimeClasspathElements(); for (Iterator i = classpathElements.iterator(); i.hasNext();) { String element = (String) i.next(); if (element.endsWith(".jar")) { FileUtils.copyFileToDirectory(element, runtimeLibraryDirectory.getAbsolutePath()); } } // copy runtime to META-INF in build dir FileUtils.copyFileToDirectory(runtimeFile, runtimeMetaDirectory); } catch (DependencyResolutionRequiredException der) { throw new MavenRuntimeBuilderException("error getting dependencies while building runtime", der); } catch (IOException ex) { throw new MavenRuntimeBuilderException("Exception while building the runtime", ex); } }
From source file:org.codehaus.mojo.webtest.components.AntExecutor.java
License:Apache License
/** * Run an ANT script within the JVM../*from w w w .ja v a 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); }