aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java
blob: 9ee658ddc22996b23dce6944729d14dea2b0774f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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.classanalysis.Analyze;
import com.yahoo.container.plugin.classanalysis.ClassFileMetaData;
import com.yahoo.container.plugin.classanalysis.PackageTally;
import com.yahoo.container.plugin.osgi.ExportPackages.Export;
import com.yahoo.container.plugin.osgi.ImportPackages;
import com.yahoo.container.plugin.util.Artifacts;
import com.yahoo.container.plugin.util.TestBundleDependencyScopeTranslator;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static com.yahoo.container.plugin.bundle.AnalyzeBundle.exportedPackagesAggregated;
import static com.yahoo.container.plugin.util.TestBundleUtils.outputDirectory;
import static com.yahoo.container.plugin.osgi.ExportPackages.exportsByPackageName;
import static com.yahoo.container.plugin.osgi.ImportPackages.calculateImports;
import static com.yahoo.container.plugin.util.Files.allDescendantFiles;

/**
 * @author bjorncs
 */
@Mojo(name = "generate-test-bundle-osgi-manifest", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
public class GenerateTestBundleOsgiManifestMojo extends AbstractGenerateOsgiManifestMojo {

    @Parameter
    private String testBundleScopeOverrides;

    public void execute() throws MojoExecutionException {
        try {
            Artifacts.ArtifactSet artifactSet = Artifacts.getArtifacts(
                    project, TestBundleDependencyScopeTranslator.from(project.getArtifacts(), testBundleScopeOverrides));

            List<File> providedJars = artifactSet.getJarArtifactsProvided().stream()
                    .map(Artifact::getFile)
                    .toList();

            List<Export> exportedPackagesFromProvidedJars = exportedPackagesAggregated(providedJars);

            List<ClassFileMetaData> analyzedClasses = getProjectMainAndTestClasses();

            PackageTally projectPackages = PackageTally.fromAnalyzedClassFiles(analyzedClasses);

            PackageTally jarArtifactsToInclude = definedPackages(artifactSet.getJarArtifactsToInclude());

            PackageTally includedPackages = projectPackages.combine(jarArtifactsToInclude);

            Map<String, ImportPackages.Import> calculatedImports = calculateImports(includedPackages.referencedPackages(),
                    includedPackages.definedPackages(),
                    exportsByPackageName(exportedPackagesFromProvidedJars));

            Map<String, String> manifestContent = generateManifestContent(artifactSet.getJarArtifactsToInclude(), calculatedImports, includedPackages);
            addAdditionalManifestProperties(manifestContent, referencedTestCategories(analyzedClasses));
            createManifestFile(outputDirectory(project), manifestContent);

        } catch (Exception e) {
            throw new MojoExecutionException("Failed generating osgi manifest", e);
        }
    }

    private void addAdditionalManifestProperties(Map<String, String> manifestContent, List<String> referencedTestCategories) {
        manifestContent.put("X-JDisc-Test-Bundle-Version", "1.0");
        manifestContent.put("X-JDisc-Test-Bundle-Categories", String.join(",", referencedTestCategories));
    }

    private List<ClassFileMetaData> getProjectMainAndTestClasses() {
        return Stream.concat(allDescendantFiles(new File(project.getBuild().getOutputDirectory())),
                             allDescendantFiles(new File(project.getBuild().getTestOutputDirectory())))
                     .filter(file -> file.getName().endsWith(".class"))
                     .map(classFile -> Analyze.analyzeClass(classFile, null))
                     .toList();
    }

    private static List<String> referencedTestCategories(List<ClassFileMetaData> analyzedClasses) {
        Set<String> referencedClasses = new HashSet<>();
        for (ClassFileMetaData data : analyzedClasses)
            referencedClasses.addAll(data.getReferencedClasses());

        return Stream.of("ai.vespa.hosted.cd.SystemTest",
                         "ai.vespa.hosted.cd.StagingSetup",
                         "ai.vespa.hosted.cd.StagingTest",
                         "ai.vespa.hosted.cd.ProductionTest")
                     .filter(referencedClasses::contains)
                     .map(name -> name.substring(19))
                     .toList();
    }

}