Example usage for org.apache.maven.project.artifact AttachedArtifact setFile

List of usage examples for org.apache.maven.project.artifact AttachedArtifact setFile

Introduction

In this page you can find the example usage for org.apache.maven.project.artifact AttachedArtifact setFile.

Prototype

public void setFile(File file) 

Source Link

Usage

From source file:de.hwbllmnn.maven.JSSourceArchiveMojo.java

License:GNU General Public License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    File dir = new File(project.getBasedir(), "src/main/javascript");
    ZipOutputStream out = null;/*from  w w  w.java2  s.c  o m*/
    AttachedArtifact artifact = new AttachedArtifact(project.getArtifact(), "jslib", "source",
            new DefaultArtifactHandler("jslib"));

    List<File> list = new LinkedList<File>();

    for (Object o : project.getDependencyArtifacts()) {
        Artifact a = (Artifact) o;
        if (a.getType().equals("jslib")) {
            Artifact source = artifactFactory.createArtifactWithClassifier(a.getGroupId(), a.getArtifactId(),
                    a.getVersion(), "jslib", "source");
            try {
                artifactResolver.resolve(source, project.getRemoteArtifactRepositories(), localRepository);
            } catch (ArtifactResolutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ArtifactNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            list.add(source.getFile());
        }
    }

    File target = new File(project.getBasedir(), "target");
    if (!target.exists() && !target.mkdirs()) {
        throw new MojoFailureException("Could not create target directory!");
    }
    File file = new File(project.getBasedir(),
            "target/" + project.getArtifactId() + "-" + project.getVersion() + "-source.jslib");

    artifact.setFile(file);
    project.addAttachedArtifact(artifact);
    try {
        out = new ZipOutputStream(new FileOutputStream(file));
        zip(dir, out, dir.toURI());
        for (File f : list) {
            File tmp = new File(target, f.getName());
            FileInputStream in = new FileInputStream(f);
            try {
                unzip(in, tmp);
                zip(tmp, out, tmp.getAbsoluteFile().toURI());
            } finally {
                closeQuietly(in);
            }
        }
    } catch (IOException e) {
        getLog().error(e);
    } finally {
        closeQuietly(out);
    }
}