@Test(expected = Exception.class) public void testAssembleOpear_asJar_stillDir() throws Exception { final File testOutDir = new File(testOutBaseDir, "testAssembleOpear_asJar_stillDir"); FileUtils.deleteDirectory(testOutDir); testOutDir.mkdirs(); final OpearPackageMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); mojo.project = project; when(project.getPackaging()).thenReturn("jar"); final String expectGroupId = "com.example.testAssembleOpear"; final String expectArtifactId = "assembly1"; final String expectVersion = "1.0-SNAPSHOT"; when(project.getGroupId()).thenReturn(expectGroupId); when(project.getArtifactId()).thenReturn(expectArtifactId); when(project.getVersion()).thenReturn(expectVersion); final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); final RepositorySystem repositorySystem = mock(RepositorySystem.class); mojo.repositorySystem = repositorySystem; final File basedir = new File(testOutDir, "basedir"); final File projectFile = new File(basedir, "pom.xml"); FileUtils.touch(projectFile); when(project.getFile()).thenReturn(projectFile); final Artifact projectArtifact = mock(Artifact.class); when(projectArtifact.getType()).thenReturn("jar"); when(project.getArtifact()).thenReturn(projectArtifact); final File target = new File(basedir, "target"); target.mkdirs(); mojo.outputDirectory = target; final File targetClasses = new File(target, "classes"); targetClasses.mkdirs(); when(projectArtifact.getFile()).thenReturn(targetClasses); final String expectFinalName = expectArtifactId + "-" + expectVersion; mojo.finalName = expectFinalName; final String expectOakpalVersion = "0.31415926535898"; final Dependency oakpalCoreDep = new DependencyFilter() .withGroupId(OAKPAL_GROUP_ID) .withArtifactId(OAKPAL_CORE_ARTIFACT_ID) .withVersion(expectOakpalVersion) .toDependency(); final File oakpalDepFile = new File(testOutDir, "oakpal-core.jar"); final Artifact oakpalArt = mock(Artifact.class); when(oakpalArt.getGroupId()).thenReturn(OAKPAL_GROUP_ID); when(oakpalArt.getArtifactId()).thenReturn(OAKPAL_CORE_ARTIFACT_ID); when(oakpalArt.getVersion()).thenReturn(expectOakpalVersion); when(oakpalArt.getScope()).thenReturn("compile"); when(oakpalArt.getFile()).thenReturn(oakpalDepFile); final Dependency compileDep = new DependencyFilter().toDependency("compile"); final File compileDepFile = new File(testOutDir, "compile-scope.jar"); FileUtils.touch(compileDepFile); final Artifact compileArt = mock(Artifact.class); when(compileArt.getGroupId()).thenReturn(OAKPAL_GROUP_ID); when(compileArt.getArtifactId()).thenReturn("compile-scope"); when(compileArt.getScope()).thenReturn("compile"); when(compileArt.getFile()).thenReturn(compileDepFile); when(project.getDependencies()).thenReturn(Arrays.asList(oakpalCoreDep, compileDep)); doAnswer(call -> { final Dependency input = call.getArgument(0, Dependency.class); if (input == oakpalCoreDep) { return oakpalArt; } else if (input == compileDep) { return compileArt; } else { return null; } }).when(repositorySystem).createDependencyArtifact(any(Dependency.class)); doAnswer(call -> { final ArtifactResolutionRequest request = call.getArgument(0, ArtifactResolutionRequest.class); final ArtifactResolutionResult result = mock(ArtifactResolutionResult.class); when(result.getArtifacts()).thenReturn(Collections.singleton(request.getArtifact())); return result; }).when(repositorySystem).resolve(any(ArtifactResolutionRequest.class)); final String expectFilename = "sample-plan.json"; final File samplePlanFile = new File(srcDir, expectFilename); mojo.planFile = samplePlanFile; when(project.clone()).thenReturn(project); mojo.assembleOpear(); }
File assembleOpear() throws Exception { final String prefix = "lib/"; if (this.outputDirectory == null) { throw new IllegalStateException("outputDirectory should not be null"); } final File finalFile = new File(this.outputDirectory, this.finalName + "." + OPEAR); final File planPrep = new File(this.outputDirectory, "plans-tmp-" + this.finalName); FileUtils.deleteDirectory(planPrep); planPrep.mkdirs(); final Result<List<String>> planNames = this.shrinkWrapPlans(planPrep); planNames.throwCause(Exception.class); final MavenArchiveConfiguration conf = new MavenArchiveConfiguration(); conf.addManifestEntry(Opear.MF_BUNDLE_SYMBOLICNAME, getBundleSymbolicName()); conf.addManifestEntry(Opear.MF_OAKPAL_VERSION, getOakpalCoreVersion()); conf.addManifestEntry(Opear.MF_OAKPAL_PLAN, Util.escapeManifestHeaderValues(planNames.getOrDefault(Collections.emptyList()))); OpearArchiver opear = new OpearArchiver(); opear.addFileSet(DefaultFileSet.fileSet(planPrep)); List<File> embeddedFiles = new ArrayList<>(); if ("jar".equals(project.getPackaging()) && project.getArtifact().getFile().exists()) { if (project.getArtifact().getFile().isDirectory()) { throw new Exception("cannot embed project artifact while it is a directory."); } embeddedFiles.add(project.getArtifact().getFile()); } else if (OPEAR.equals(project.getPackaging()) && new File(project.getBuild().getOutputDirectory()).isDirectory()) { embeddedFiles.add(new File(project.getBuild().getOutputDirectory())); } embeddedFiles.addAll(getEmbeddedLibraries()); Map<String, File> destFiles = mapDestFileNames(new LinkedHashMap<>(), embeddedFiles, File::getName); final String scanClassPath = Util.escapeManifestHeaderValue(destFiles.entrySet().stream() .map(entry -> prefix + entry.getKey()) .toArray(String[]::new)); conf.addManifestEntry(Opear.MF_CLASS_PATH, scanClassPath); destFiles.entrySet().stream().filter(testValue(File::isFile)).forEachOrdered(entry -> { opear.addFile(entry.getValue(), prefix + entry.getKey()); }); destFiles.entrySet().stream().filter(testValue(File::isDirectory)).forEachOrdered(entry -> { opear.addFileSet(new DefaultFileSet(entry.getValue()).prefixed(prefix + entry.getKey() + "/")); }); MavenArchiver mavenArchiver = new MavenArchiver(); mavenArchiver.setArchiver(opear); mavenArchiver.setOutputFile(finalFile); mavenArchiver.createArchive(getSession(), project, conf); return finalFile; }
OpearPackageMojo extends AbstractCommonMojo { File assembleOpear() throws Exception { final String prefix = "lib/"; if (this.outputDirectory == null) { throw new IllegalStateException("outputDirectory should not be null"); } final File finalFile = new File(this.outputDirectory, this.finalName + "." + OPEAR); final File planPrep = new File(this.outputDirectory, "plans-tmp-" + this.finalName); FileUtils.deleteDirectory(planPrep); planPrep.mkdirs(); final Result<List<String>> planNames = this.shrinkWrapPlans(planPrep); planNames.throwCause(Exception.class); final MavenArchiveConfiguration conf = new MavenArchiveConfiguration(); conf.addManifestEntry(Opear.MF_BUNDLE_SYMBOLICNAME, getBundleSymbolicName()); conf.addManifestEntry(Opear.MF_OAKPAL_VERSION, getOakpalCoreVersion()); conf.addManifestEntry(Opear.MF_OAKPAL_PLAN, Util.escapeManifestHeaderValues(planNames.getOrDefault(Collections.emptyList()))); OpearArchiver opear = new OpearArchiver(); opear.addFileSet(DefaultFileSet.fileSet(planPrep)); List<File> embeddedFiles = new ArrayList<>(); if ("jar".equals(project.getPackaging()) && project.getArtifact().getFile().exists()) { if (project.getArtifact().getFile().isDirectory()) { throw new Exception("cannot embed project artifact while it is a directory."); } embeddedFiles.add(project.getArtifact().getFile()); } else if (OPEAR.equals(project.getPackaging()) && new File(project.getBuild().getOutputDirectory()).isDirectory()) { embeddedFiles.add(new File(project.getBuild().getOutputDirectory())); } embeddedFiles.addAll(getEmbeddedLibraries()); Map<String, File> destFiles = mapDestFileNames(new LinkedHashMap<>(), embeddedFiles, File::getName); final String scanClassPath = Util.escapeManifestHeaderValue(destFiles.entrySet().stream() .map(entry -> prefix + entry.getKey()) .toArray(String[]::new)); conf.addManifestEntry(Opear.MF_CLASS_PATH, scanClassPath); destFiles.entrySet().stream().filter(testValue(File::isFile)).forEachOrdered(entry -> { opear.addFile(entry.getValue(), prefix + entry.getKey()); }); destFiles.entrySet().stream().filter(testValue(File::isDirectory)).forEachOrdered(entry -> { opear.addFileSet(new DefaultFileSet(entry.getValue()).prefixed(prefix + entry.getKey() + "/")); }); MavenArchiver mavenArchiver = new MavenArchiver(); mavenArchiver.setArchiver(opear); mavenArchiver.setOutputFile(finalFile); mavenArchiver.createArchive(getSession(), project, conf); return finalFile; } }
OpearPackageMojo extends AbstractCommonMojo { File assembleOpear() throws Exception { final String prefix = "lib/"; if (this.outputDirectory == null) { throw new IllegalStateException("outputDirectory should not be null"); } final File finalFile = new File(this.outputDirectory, this.finalName + "." + OPEAR); final File planPrep = new File(this.outputDirectory, "plans-tmp-" + this.finalName); FileUtils.deleteDirectory(planPrep); planPrep.mkdirs(); final Result<List<String>> planNames = this.shrinkWrapPlans(planPrep); planNames.throwCause(Exception.class); final MavenArchiveConfiguration conf = new MavenArchiveConfiguration(); conf.addManifestEntry(Opear.MF_BUNDLE_SYMBOLICNAME, getBundleSymbolicName()); conf.addManifestEntry(Opear.MF_OAKPAL_VERSION, getOakpalCoreVersion()); conf.addManifestEntry(Opear.MF_OAKPAL_PLAN, Util.escapeManifestHeaderValues(planNames.getOrDefault(Collections.emptyList()))); OpearArchiver opear = new OpearArchiver(); opear.addFileSet(DefaultFileSet.fileSet(planPrep)); List<File> embeddedFiles = new ArrayList<>(); if ("jar".equals(project.getPackaging()) && project.getArtifact().getFile().exists()) { if (project.getArtifact().getFile().isDirectory()) { throw new Exception("cannot embed project artifact while it is a directory."); } embeddedFiles.add(project.getArtifact().getFile()); } else if (OPEAR.equals(project.getPackaging()) && new File(project.getBuild().getOutputDirectory()).isDirectory()) { embeddedFiles.add(new File(project.getBuild().getOutputDirectory())); } embeddedFiles.addAll(getEmbeddedLibraries()); Map<String, File> destFiles = mapDestFileNames(new LinkedHashMap<>(), embeddedFiles, File::getName); final String scanClassPath = Util.escapeManifestHeaderValue(destFiles.entrySet().stream() .map(entry -> prefix + entry.getKey()) .toArray(String[]::new)); conf.addManifestEntry(Opear.MF_CLASS_PATH, scanClassPath); destFiles.entrySet().stream().filter(testValue(File::isFile)).forEachOrdered(entry -> { opear.addFile(entry.getValue(), prefix + entry.getKey()); }); destFiles.entrySet().stream().filter(testValue(File::isDirectory)).forEachOrdered(entry -> { opear.addFileSet(new DefaultFileSet(entry.getValue()).prefixed(prefix + entry.getKey() + "/")); }); MavenArchiver mavenArchiver = new MavenArchiver(); mavenArchiver.setArchiver(opear); mavenArchiver.setOutputFile(finalFile); mavenArchiver.createArchive(getSession(), project, conf); return finalFile; }  }
OpearPackageMojo extends AbstractCommonMojo { File assembleOpear() throws Exception { final String prefix = "lib/"; if (this.outputDirectory == null) { throw new IllegalStateException("outputDirectory should not be null"); } final File finalFile = new File(this.outputDirectory, this.finalName + "." + OPEAR); final File planPrep = new File(this.outputDirectory, "plans-tmp-" + this.finalName); FileUtils.deleteDirectory(planPrep); planPrep.mkdirs(); final Result<List<String>> planNames = this.shrinkWrapPlans(planPrep); planNames.throwCause(Exception.class); final MavenArchiveConfiguration conf = new MavenArchiveConfiguration(); conf.addManifestEntry(Opear.MF_BUNDLE_SYMBOLICNAME, getBundleSymbolicName()); conf.addManifestEntry(Opear.MF_OAKPAL_VERSION, getOakpalCoreVersion()); conf.addManifestEntry(Opear.MF_OAKPAL_PLAN, Util.escapeManifestHeaderValues(planNames.getOrDefault(Collections.emptyList()))); OpearArchiver opear = new OpearArchiver(); opear.addFileSet(DefaultFileSet.fileSet(planPrep)); List<File> embeddedFiles = new ArrayList<>(); if ("jar".equals(project.getPackaging()) && project.getArtifact().getFile().exists()) { if (project.getArtifact().getFile().isDirectory()) { throw new Exception("cannot embed project artifact while it is a directory."); } embeddedFiles.add(project.getArtifact().getFile()); } else if (OPEAR.equals(project.getPackaging()) && new File(project.getBuild().getOutputDirectory()).isDirectory()) { embeddedFiles.add(new File(project.getBuild().getOutputDirectory())); } embeddedFiles.addAll(getEmbeddedLibraries()); Map<String, File> destFiles = mapDestFileNames(new LinkedHashMap<>(), embeddedFiles, File::getName); final String scanClassPath = Util.escapeManifestHeaderValue(destFiles.entrySet().stream() .map(entry -> prefix + entry.getKey()) .toArray(String[]::new)); conf.addManifestEntry(Opear.MF_CLASS_PATH, scanClassPath); destFiles.entrySet().stream().filter(testValue(File::isFile)).forEachOrdered(entry -> { opear.addFile(entry.getValue(), prefix + entry.getKey()); }); destFiles.entrySet().stream().filter(testValue(File::isDirectory)).forEachOrdered(entry -> { opear.addFileSet(new DefaultFileSet(entry.getValue()).prefixed(prefix + entry.getKey() + "/")); }); MavenArchiver mavenArchiver = new MavenArchiver(); mavenArchiver.setArchiver(opear); mavenArchiver.setOutputFile(finalFile); mavenArchiver.createArchive(getSession(), project, conf); return finalFile; }  @Override void execute(); }
OpearPackageMojo extends AbstractCommonMojo { File assembleOpear() throws Exception { final String prefix = "lib/"; if (this.outputDirectory == null) { throw new IllegalStateException("outputDirectory should not be null"); } final File finalFile = new File(this.outputDirectory, this.finalName + "." + OPEAR); final File planPrep = new File(this.outputDirectory, "plans-tmp-" + this.finalName); FileUtils.deleteDirectory(planPrep); planPrep.mkdirs(); final Result<List<String>> planNames = this.shrinkWrapPlans(planPrep); planNames.throwCause(Exception.class); final MavenArchiveConfiguration conf = new MavenArchiveConfiguration(); conf.addManifestEntry(Opear.MF_BUNDLE_SYMBOLICNAME, getBundleSymbolicName()); conf.addManifestEntry(Opear.MF_OAKPAL_VERSION, getOakpalCoreVersion()); conf.addManifestEntry(Opear.MF_OAKPAL_PLAN, Util.escapeManifestHeaderValues(planNames.getOrDefault(Collections.emptyList()))); OpearArchiver opear = new OpearArchiver(); opear.addFileSet(DefaultFileSet.fileSet(planPrep)); List<File> embeddedFiles = new ArrayList<>(); if ("jar".equals(project.getPackaging()) && project.getArtifact().getFile().exists()) { if (project.getArtifact().getFile().isDirectory()) { throw new Exception("cannot embed project artifact while it is a directory."); } embeddedFiles.add(project.getArtifact().getFile()); } else if (OPEAR.equals(project.getPackaging()) && new File(project.getBuild().getOutputDirectory()).isDirectory()) { embeddedFiles.add(new File(project.getBuild().getOutputDirectory())); } embeddedFiles.addAll(getEmbeddedLibraries()); Map<String, File> destFiles = mapDestFileNames(new LinkedHashMap<>(), embeddedFiles, File::getName); final String scanClassPath = Util.escapeManifestHeaderValue(destFiles.entrySet().stream() .map(entry -> prefix + entry.getKey()) .toArray(String[]::new)); conf.addManifestEntry(Opear.MF_CLASS_PATH, scanClassPath); destFiles.entrySet().stream().filter(testValue(File::isFile)).forEachOrdered(entry -> { opear.addFile(entry.getValue(), prefix + entry.getKey()); }); destFiles.entrySet().stream().filter(testValue(File::isDirectory)).forEachOrdered(entry -> { opear.addFileSet(new DefaultFileSet(entry.getValue()).prefixed(prefix + entry.getKey() + "/")); }); MavenArchiver mavenArchiver = new MavenArchiver(); mavenArchiver.setArchiver(opear); mavenArchiver.setOutputFile(finalFile); mavenArchiver.createArchive(getSession(), project, conf); return finalFile; }  @Override void execute(); static final String OPEAR; }