Search in sources :

Example 1 with BSNRenamer

use of org.apache.sling.commons.osgi.BSNRenamer in project sling by apache.

the class PreparePackageMojo method buildContentsMap.

/**
     * Build a list of all artifacts from this run mode
     */
private void buildContentsMap(final Model model, final RunMode runMode, final Map<String, File> contentsMap, final boolean isBoot) throws MojoExecutionException {
    for (final ArtifactGroup group : runMode.getArtifactGroups()) {
        for (final org.apache.sling.provisioning.model.Artifact a : group) {
            Artifact artifact = null;
            if (a.getGroupId().equals(this.project.getGroupId()) && a.getArtifactId().equals(this.project.getArtifactId()) && a.getVersion().equals(this.project.getVersion())) {
                for (final Artifact projectArtifact : this.project.getAttachedArtifacts()) {
                    if (projectArtifact.getClassifier().equals(a.getClassifier())) {
                        artifact = projectArtifact;
                        break;
                    }
                }
                // check if the artifact is bound already?
                if (project.getArtifact().getFile().exists()) {
                    if (a.getClassifier() != null) {
                        if (a.getClassifier().equals(project.getArtifact().getClassifier())) {
                            artifact = project.getArtifact();
                        } else {
                            throw new MojoExecutionException("Unable to find artifact from same project with the given classifier: " + a.toMvnUrl());
                        }
                    } else {
                        if (project.getArtifact().getClassifier() == null) {
                            artifact = project.getArtifact();
                        } else {
                            throw new MojoExecutionException("Unable to find artifact with no classifier from same project, because the main artifact is bound to classifier " + project.getArtifact().getClassifier());
                        }
                    }
                } else {
                    throw new MojoExecutionException("You must not reference artifact " + a.toMvnUrl() + " from the model which is not yet available in the phase '" + mojoExecution.getLifecyclePhase() + ". Make sure to execute this goal after phase 'package'");
                }
            } else {
                artifact = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver, a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getType(), a.getClassifier());
            }
            File artifactFile = artifact.getFile();
            String newBSN = a.getMetadata().get("bundle:rename-bsn");
            if (newBSN != null) {
                try {
                    getTmpDir().mkdirs();
                    artifactFile = new BSNRenamer(artifactFile, getTmpDir(), newBSN).process();
                } catch (IOException e) {
                    throw new MojoExecutionException("Unable to rename bundle BSN to " + newBSN + " for " + artifactFile, e);
                }
            }
            contentsMap.put(getPathForArtifact(group.getStartLevel(), artifactFile.getName(), runMode, isBoot), artifactFile);
        }
    }
    final File rootConfDir = new File(this.getTmpDir(), "global-config");
    boolean hasConfig = false;
    for (final Configuration config : runMode.getConfigurations()) {
        // skip special configurations
        if (config.isSpecial()) {
            continue;
        }
        final String configPath = getPathForConfiguration(config, runMode);
        final File configFile = new File(rootConfDir, configPath);
        getLog().debug(String.format("Creating configuration at %s", configFile.getPath()));
        configFile.getParentFile().mkdirs();
        try {
            final FileOutputStream os = new FileOutputStream(configFile);
            try {
                ConfigurationHandler.write(os, config.getProperties());
            } finally {
                os.close();
            }
        } catch (final IOException e) {
            throw new MojoExecutionException("Unable to write configuration to " + configFile, e);
        }
        hasConfig = true;
    }
    if (hasConfig) {
        contentsMap.put(BASE_DESTINATION, rootConfDir);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Configuration(org.apache.sling.provisioning.model.Configuration) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) FileOutputStream(java.io.FileOutputStream) BSNRenamer(org.apache.sling.commons.osgi.BSNRenamer) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 BSNRenamer (org.apache.sling.commons.osgi.BSNRenamer)1 ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)1 Configuration (org.apache.sling.provisioning.model.Configuration)1