Example usage for org.apache.maven.artifact DefaultArtifact getFile

List of usage examples for org.apache.maven.artifact DefaultArtifact getFile

Introduction

In this page you can find the example usage for org.apache.maven.artifact DefaultArtifact getFile.

Prototype

public File getFile() 

Source Link

Usage

From source file:com.agileapes.couteau.maven.sample.task.CompileCodeTask.java

License:Open Source License

/**
 * This method will determine the classpath argument passed to the JavaC compiler used by this
 * implementation//from ww w .  j a  v  a 2s. c o m
 * @return the classpath
 * @throws org.apache.maven.artifact.DependencyResolutionRequiredException if the resolution of classpath requires a prior
 * resolution of dependencies for the target project
 */
private String getClassPath(SampleExecutor executor) throws DependencyResolutionRequiredException {
    //Adding compile time dependencies for the project
    final List elements = executor.getProject().getCompileClasspathElements();
    final Set<File> classPathElements = new HashSet<File>();
    //noinspection unchecked
    classPathElements.addAll(elements);
    //Adding runtime dependencies available to the target project
    final ClassLoader loader = executor.getProjectClassLoader();
    URL[] urls = new URL[0];
    if (loader instanceof URLClassLoader) {
        urls = ((URLClassLoader) loader).getURLs();
    } else if (loader instanceof ConfigurableClassLoader) {
        urls = ((ConfigurableClassLoader) loader).getUrls();
    }
    for (URL url : urls) {
        try {
            final File file = new File(url.toURI());
            if (file.exists()) {
                classPathElements.add(file);
            }
        } catch (Throwable ignored) {
        }
    }
    //Adding dependency artifacts for the target project
    for (Object dependencyArtifact : executor.getProject().getDependencyArtifacts()) {
        if (!(dependencyArtifact instanceof DefaultArtifact)) {
            continue;
        }
        DefaultArtifact artifact = (DefaultArtifact) dependencyArtifact;
        if (artifact.getFile() != null) {
            classPathElements.add(artifact.getFile());
        }
    }
    return StringUtils.collectionToDelimitedString(classPathElements, File.pathSeparator);
}

From source file:org.apache.activemq.artemis.server.SpawnedVMSupport.java

License:Apache License

public static Process spawnVM(List<DefaultArtifact> arts, final String logName, final String className,
        final Properties properties, final boolean logOutput, final String success, final String failure,
        final String workDir, final String configDir, boolean debug, final String... args) throws Exception {
    StringBuffer sb = new StringBuffer();

    sb.append("java").append(' ');
    StringBuffer props = new StringBuffer();
    if (properties != null) {
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            props.append("-D").append(entry.getKey()).append("=").append(entry.getValue()).append(" ");
        }/*from w  w w. jav a  2 s .c om*/
    }
    String vmarg = props.toString();
    String osName = System.getProperty("os.name");
    osName = (osName != null) ? osName.toLowerCase() : "";
    boolean isWindows = osName.contains("win");
    if (isWindows) {
        vmarg = vmarg.replaceAll("/", "\\\\");
    }
    sb.append(vmarg).append(" ");
    String pathSeparater = System.getProperty("path.separator");
    StringBuilder classpath = new StringBuilder();
    for (DefaultArtifact artifact : arts) {
        classpath.append(artifact.getFile().getAbsolutePath()).append(pathSeparater);
    }
    classpath.append(configDir).append(pathSeparater);

    if (isWindows) {
        sb.append("-cp").append(" \"").append(classpath.toString()).append("\" ");
    } else {
        sb.append("-cp").append(" ").append(classpath.toString()).append(" ");
    }

    // FIXME - not good to assume path separator
    String libPath = "-Djava.library.path=" + System.getProperty("java.library.path", "./native/bin");
    if (isWindows) {
        libPath = libPath.replaceAll("/", "\\\\");
        libPath = "\"" + libPath + "\"";
    }
    sb.append("-Djava.library.path=").append(libPath).append(" ");
    if (debug) {
        sb.append("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 ");
    }

    sb.append(className).append(' ');

    for (String arg : args) {
        sb.append(arg).append(' ');
    }

    String commandLine = sb.toString();

    //SpawnedVMSupport.log.trace("command line: " + commandLine);

    Process process = Runtime.getRuntime().exec(commandLine, null, new File(workDir));

    //SpawnedVMSupport.log.trace("process: " + process);

    CountDownLatch latch = new CountDownLatch(1);

    ProcessLogger outputLogger = new ProcessLogger(logOutput, process.getInputStream(), logName, false, success,
            failure, latch);
    outputLogger.start();

    // Adding a reader to System.err, so the VM won't hang on a System.err.println as identified on this forum thread:
    // http://www.jboss.org/index.html?module=bb&op=viewtopic&t=151815
    ProcessLogger errorLogger = new ProcessLogger(true, process.getErrorStream(), logName, true, success,
            failure, latch);
    errorLogger.start();

    if (!latch.await(60, TimeUnit.SECONDS)) {
        process.destroy();
        throw new RuntimeException("Timed out waiting for server to start");
    }

    if (outputLogger.failed || errorLogger.failed) {
        try {
            process.destroy();
        } catch (Throwable e) {
        }
        throw new RuntimeException("server failed to start");
    }
    return process;
}

From source file:org.clickframes.mavenplugin.ClickframesGenPlugin.java

License:Open Source License

/**
 * Maven's answer to/*from ww  w.j a v a 2  s  .com*/
 * System.getProperty("java.class.path").split(System.getProperty
 * ("path.separator"));
 * 
 * @return list of each jar included in maven classpath.
 */
private List<String> getMavenRuntimeClasspathEntries() {
    List<String> entries = new ArrayList<String>();
    for (DefaultArtifact o : artifacts) {
        entries.add(o.getFile().getAbsolutePath());
    }
    return entries;
}