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

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

Introduction

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

Prototype

public void setArtifactHandler(ArtifactHandler artifactHandler) 

Source Link

Usage

From source file:org.rioproject.tools.maven.OarMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    File opstringFile = getOpStringFile();
    if (!opstringFile.exists()) {
        getLog().debug("The opstring [" + opstring + "] does not exist, cannot build OAR");
        return;/*www . j a va2  s  .  co  m*/
    }
    File contentDirectory = new File(project.getBuild().getOutputDirectory());
    String outputDir = project.getBuild().getDirectory();
    getLog().debug("Project Name    : " + project.getName());
    getLog().debug("Artifact ID     : " + project.getArtifactId());
    getLog().debug("Build Directory : " + outputDir);
    getLog().debug("Project Base Dir : " + project.getBasedir());
    getLog().debug("Build Dir        : " + project.getBuild().getDirectory());
    getLog().debug("Build Output Dir : " + contentDirectory.getPath());

    List<Repository> remoteRepositories = new ArrayList<Repository>();
    try {
        for (Object o : project.getRemoteArtifactRepositories()) {
            Method getId = o.getClass().getMethod("getId");
            String id = (String) getId.invoke(o);
            if (id.equals("central"))
                continue;
            Repository repository = new Repository();
            repository.setId(id);

            Method getUrl = o.getClass().getMethod("getUrl");
            repository.setUrl((String) getUrl.invoke(o));

            repository.setSnapshots(repositoryPolicySupported(o, "getSnapshots"));
            repository.setSnapshotUpdatePolicy(repositoryPolicyUpdatePolicy(o, true));
            repository.setSnapshotChecksumPolicy(repositoryPolicyChecksumPolicy(o, true));

            repository.setReleases(repositoryPolicySupported(o, "getReleases"));
            repository.setReleaseUpdatePolicy(repositoryPolicyUpdatePolicy(o, false));
            repository.setReleaseChecksumPolicy(repositoryPolicyChecksumPolicy(o, false));
            remoteRepositories.add(repository);
        }
    } catch (NoSuchMethodException e) {
        throw new MojoExecutionException("Building Repository list", e);
    } catch (InvocationTargetException e) {
        throw new MojoExecutionException("Building Repository list", e);
    } catch (IllegalAccessException e) {
        throw new MojoExecutionException("Building Repository list", e);
    }

    getLog().info("Building OAR: " + getOarFileName());
    if (getOarName() == null)
        oarName = project.getArtifactId();
    Jar oar = new Jar();
    oar.setProject(antProject);
    File oarFile = new File(getOarFileName());
    oar.setDestFile(oarFile);

    /* Add repository configuration */
    if (encodeRepositories) {
        RepositoryEncoder repositoryEncoder = new RepositoryEncoder();
        File repoConfiguration = new File(System.getProperty("java.io.tmpdir"), "repositories.xml");
        repoConfiguration.deleteOnExit();
        repositoryEncoder.encode(remoteRepositories, repoConfiguration);
        ZipFileSet fileSetRepositoryConfig = new ZipFileSet();
        fileSetRepositoryConfig.setDir(repoConfiguration.getParentFile());
        fileSetRepositoryConfig.setIncludes(repoConfiguration.getName());
        oar.addZipfileset(fileSetRepositoryConfig);
    }

    /* Add the pom */
    File projectPath = project.getBasedir();
    String pomName = project.getBuild().getFinalName();
    File tempPom = new File(System.getProperty("java.io.tmpdir"), pomName + ".pom");
    tempPom.deleteOnExit();
    File projectPom = new File(projectPath, "pom.xml");
    try {
        FileUtils.copyFile(projectPom, tempPom);
    } catch (IOException e) {
        throw new MojoExecutionException("could not create temp pom", e);
    }
    ZipFileSet fileSetPom = new ZipFileSet();
    fileSetPom.setDir(tempPom.getParentFile());
    fileSetPom.setIncludes(tempPom.getName());
    oar.addZipfileset(fileSetPom);

    // add the opstring
    ZipFileSet fileSetOpstring = new ZipFileSet();
    fileSetOpstring.setDir(opstringFile.getParentFile());
    fileSetOpstring.setIncludes(opstringFile.getName());
    oar.addZipfileset(fileSetOpstring);

    List<String> attached = new ArrayList<String>();
    for (Object o : project.getAttachedArtifacts()) {
        AttachedArtifact a = (AttachedArtifact) o;
        StringBuilder sb = new StringBuilder();
        sb.append(a.getGroupId());
        sb.append(":");
        sb.append(a.getArtifactId());
        sb.append(":");
        if (a.getClassifier() != null && a.getClassifier().length() > 0) {
            sb.append(a.getClassifier());
            sb.append(":");
        }
        sb.append(a.getVersion());
        attached.add(sb.toString());
    }

    // add the manifest
    Manifest manifest = new Manifest();
    try {
        manifest.addConfiguredAttribute(new Manifest.Attribute("OAR-Name", oarName));
        manifest.addConfiguredAttribute(new Manifest.Attribute("OAR-Version", "1"));
        manifest.addConfiguredAttribute(
                new Manifest.Attribute("OAR-OperationalString", getOpStringFile().getName()));
        manifest.addConfiguredAttribute(new Manifest.Attribute("OAR-Activation", activation));
        if (!attached.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (String s : attached) {
                if (sb.length() > 0)
                    sb.append(",");
                sb.append(s);
            }
            manifest.addConfiguredAttribute(new Manifest.Attribute("OAR-Artifacts", sb.toString()));
        }
    } catch (ManifestException e) {
        getLog().error("Can't generate OAR manifest", e);
        return;
    }
    try {
        oar.addConfiguredManifest(manifest);
    } catch (ManifestException e) {
        getLog().error("Can't add manifest into OAR", e);
        return;
    }

    /* Make sure target directory exists */
    File target = oarFile.getParentFile();
    if (!target.exists()) {
        if (target.mkdirs())
            getLog().debug("Created " + target.getPath());
    }
    /* Execute the Ant task */
    oar.execute();
    if (!project.getPackaging().equals("oar")) {
        getLog().info("Attaching artifact " + oarFile.getName() + " as type oar");
        projectHelper.attachArtifact(project, "oar", null, oarFile);
        for (Object o : project.getAttachedArtifacts()) {
            if (o instanceof AttachedArtifact) {
                AttachedArtifact a = (AttachedArtifact) o;
                if (a.getType().equals("oar")) {
                    a.setArtifactHandler(new DefaultArtifactHandler("oar"));
                    getLog().debug("Artifact : " + a + ", type: " + a.getType() + ", "
                            + "artifact handler extension: " + a.getArtifactHandler().getExtension());
                }
            }
        }
    } else {
        project.getArtifact().setFile(oarFile);
    }
}