aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleContainerPluginMojo.java
blob: bb2d61932f349dc913ea06db8d2e96368f21f1df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.mojo;

import com.yahoo.container.plugin.util.Artifacts;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.jar.JarArchiver;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EnumMap;
import java.util.Map;
import java.util.jar.JarFile;

/**
 * @author Tony Vaagenes
 * @author Olli Virtanen
 * @author bjorncs
 */
@Mojo(name = "assemble-container-plugin", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class AssembleContainerPluginMojo extends AbstractAssembleBundleMojo {
    private enum Dependencies {
        WITH, WITHOUT
    }

    @Component
    private MavenProjectHelper projectHelper;

    @Parameter(alias = "UseCommonAssemblyIds", defaultValue = "false")
    private boolean useCommonAssemblyIds = false;

    @Parameter(alias = "AttachBundle", defaultValue = "false")
    private boolean attachBundleArtifact;

    @Parameter(alias = "BundleClassifier", defaultValue = "bundle")
    private String bundleClassifierName;

    @Override
    public void execute() throws MojoExecutionException {
        Map<Dependencies, String> jarSuffixes = new EnumMap<Dependencies, String>(Dependencies.class);

        if (useCommonAssemblyIds) {
            jarSuffixes.put(Dependencies.WITHOUT, ".jar");
            jarSuffixes.put(Dependencies.WITH, "-jar-with-dependencies.jar");
        } else {
            jarSuffixes.put(Dependencies.WITHOUT, "-without-dependencies.jar");
            jarSuffixes.put(Dependencies.WITH, "-deploy.jar");
        }

        Map<Dependencies, File> jarFiles = new EnumMap<Dependencies, File>(Dependencies.class);
        jarSuffixes.forEach((dep, suffix) -> {
            jarFiles.put(dep, jarFileInBuildDirectory(build().getFinalName(), suffix));
        });

        Path manifestFile = Paths.get(build().getOutputDirectory(), JarFile.MANIFEST_NAME);

        JarArchiver jarWithoutDependencies = new JarArchiver();
        addDirectory(jarWithoutDependencies, Paths.get(build().getOutputDirectory()));
        createArchive(jarWithoutDependencies, jarFiles.get(Dependencies.WITHOUT).toPath(), manifestFile);
        project.getArtifact().setFile(jarFiles.get(Dependencies.WITHOUT));

        JarArchiver jarWithDependencies = new JarArchiver();
        addDirectory(jarWithDependencies, Paths.get(build().getOutputDirectory()));
        addArtifacts(jarWithDependencies, Artifacts.getArtifactsToInclude(project));
        createArchive(jarWithDependencies, jarFiles.get(Dependencies.WITH).toPath(), manifestFile);

        if (attachBundleArtifact) {
            projectHelper.attachArtifact(project,
                                         project.getArtifact().getType(),
                                         bundleClassifierName,
                                         jarFiles.get(Dependencies.WITH));
        }
    }

    private File jarFileInBuildDirectory(String name, String suffix) { return new File(build().getDirectory(), name + suffix); }
    private Build build() { return project.getBuild(); }
}