public void apply(final Project project) { project.getPluginManager().apply(JavaBasePlugin.class); final OsgiPluginConvention osgiConvention = new OsgiPluginConvention((ProjectInternal) project); project.getConvention().getPlugins().put("osgi", osgiConvention); project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() { @Override public void execute(JavaPlugin javaPlugin) { OsgiManifest osgiManifest = osgiConvention.osgiManifest(); osgiManifest.setClassesDir(project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main").getOutput().getClassesDir()); osgiManifest.setClasspath(project.getConfigurations().getByName("runtime")); Jar jarTask = (Jar) project.getTasks().getByName("jar"); jarTask.setManifest(osgiManifest); } }); }
@Override public void apply(final ProjectInternal project) { this.project = project; project.getPluginManager().apply(JavaPlugin.class); project.getPluginManager().apply(DistributionPlugin.class); DefaultDistributionContainer defaultDistributionContainer = (DefaultDistributionContainer) project.getExtensions().findByName("distributions"); CopySpec contentSpec = defaultDistributionContainer.getByName(DistributionPlugin.MAIN_DISTRIBUTION_NAME).getContents(); Jar jar = (Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME); CopySpec childSpec = project.copySpec(); childSpec.from(jar); childSpec.from(project.file("src/dist")); CopySpec libSpec = project.copySpec(); libSpec.into("lib"); libSpec.from(project.getConfigurations().getByName("runtime")); childSpec.with(libSpec); contentSpec.with(childSpec); }
@Override public void apply(Project project) { project.getPlugins().apply(WarPlugin.class); WarAttachClassesConvention attachClassesConvention = new WarAttachClassesConvention(); War war = (War) project.getTasks().getByName(WarPlugin.WAR_TASK_NAME); war.getConvention().getPlugins().put("attachClasses", attachClassesConvention); project.afterEvaluate(p -> { if (attachClassesConvention.isAttachClasses()) { Jar jar = (Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME); jar.setClassifier(attachClassesConvention.getClassesClassifier()); project.getArtifacts().add(Dependency.ARCHIVES_CONFIGURATION, jar); } }); }
/** * @see me.seeber.gradle.plugin.AbstractProjectConfigPlugin#initialize() */ @Override protected void initialize() { getProject().getPluginManager().apply(GroovyPlugin.class); getProject().getPluginManager().apply(JavaConfigPlugin.class); DependencyHandler dependencies = getProject().getDependencies(); ExternalDependency spock = (ExternalDependency) dependencies.add("testCompile", ImmutableMap.of("group", "org.spockframework", "name", "spock-core", "version", "1.0-groovy-2.4")); spock.exclude(ImmutableMap.of("group", "org.codehaus.groovy", "module", "groovy-all")); String name = Validate.notNull(getProject().getName()); Configuration archives = getProject().getConfigurations().getByName("archives"); Jar groovydocJar = getProject().getTasks().create("groovydocJar", Jar.class); PublishArtifact groovydocArtifact = Projects.createJarPublishArtifact(getProject(), name, "groovydoc", "jar", "jar", groovydocJar); archives.getArtifacts().add(groovydocArtifact); }
@Override public void execute(Jar jarTask) { if (!RepackageTask.this.isEnabled()) { getLogger().info("Repackage disabled"); return; } Object withJarTask = RepackageTask.this.withJarTask; if (!isTaskMatch(jarTask, withJarTask)) { getLogger().info( "Jar task not repackaged (didn't match withJarTask): " + jarTask); return; } File file = jarTask.getArchivePath(); if (file.exists()) { repackage(file); } }
private boolean isTaskMatch(Jar task, Object withJarTask) { if (withJarTask == null) { if ("".equals(task.getClassifier())) { Set<Object> tasksWithCustomRepackaging = new HashSet<Object>(); for (RepackageTask repackageTask : RepackageTask.this.getProject() .getTasks().withType(RepackageTask.class)) { if (repackageTask.getWithJarTask() != null) { tasksWithCustomRepackaging .add(repackageTask.getWithJarTask()); } } return !tasksWithCustomRepackaging.contains(task); } return false; } return task.equals(withJarTask) || task.getName().equals(withJarTask); }
@Override public void apply(Project project) { project.getExtensions().create(SWARM_EXTENSION, SwarmExtension.class, project); project.afterEvaluate(__ -> { final TaskContainer tasks = project.getTasks(); final PackageTask packageTask = tasks.create(WILDFLY_SWARM_PACKAGE_TASK_NAME, PackageTask.class); final Jar archiveTask = getArchiveTask(project); if (archiveTask == null) { throw new GradleException("No suitable Archive-Task found to include in Swarm Uber-JAR."); } packageTask.jarTask(archiveTask).dependsOn(archiveTask); tasks.getByName(JavaBasePlugin.BUILD_TASK_NAME).dependsOn(packageTask); }); }
/** * Returns the most suitable Archive-Task for wrapping in the swarm jar - in the following order: * * 1. Custom-JAR-Task defined in SwarmExtension 'archiveTask' * 2. WAR-Task * 3. JAR-Task */ private Jar getArchiveTask(Project project) { TaskCollection<Jar> existingArchiveTasks = project.getTasks().withType(Jar.class); Jar customArchiveTask = project.getExtensions().getByType(SwarmExtension.class).getArchiveTask(); if (customArchiveTask != null) { return existingArchiveTasks.getByName(customArchiveTask.getName()); } else if (existingArchiveTasks.findByName(WarPlugin.WAR_TASK_NAME) != null) { return existingArchiveTasks.getByName(WarPlugin.WAR_TASK_NAME); } else if (existingArchiveTasks.findByName(JavaPlugin.JAR_TASK_NAME) != null) { return existingArchiveTasks.getByName(JavaPlugin.JAR_TASK_NAME); } return null; }
private void configureArchivesAndComponent(final Project project, final JavaPluginConvention pluginConvention) { Jar jar = project.getTasks().create(JAR_TASK_NAME, Jar.class); jar.getManifest().from(pluginConvention.getManifest()); jar.setDescription("Assembles a jar archive containing the main classes."); jar.setGroup(BasePlugin.BUILD_GROUP); jar.from(pluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput()); jar.getMetaInf().from(new Callable() { public Object call() throws Exception { return pluginConvention.getMetaInf(); } }); ArchivePublishArtifact jarArtifact = new ArchivePublishArtifact(jar); Configuration runtimeConfiguration = project.getConfigurations().getByName(RUNTIME_CONFIGURATION_NAME); runtimeConfiguration.getArtifacts().add(jarArtifact); project.getExtensions().getByType(DefaultArtifactPublicationSet.class).addCandidate(jarArtifact); project.getComponents().add(new JavaLibrary(jarArtifact, runtimeConfiguration.getAllDependencies())); }
/** * Make the given Jar task inherit its manifest from the "main" "thin" Jar task. Also set the build timestamp. * * @param pTask the executing task * @param pDepConfig the dependency configuration for which the Jar task is intended */ public void inheritManifest(@Nonnull final Jar pTask, @Nonnull final DependencyConfig pDepConfig) { pTask.doFirst(new Closure<Void>(pTask) { @Override @SuppressWarnings("MethodDoesntCallSuperMethod") public Void call() { final Jar jarTask = (Jar) getTask(TaskNames.jar, pDepConfig); pTask.setManifest(jarTask.getManifest()); addBuildTimestamp(pTask.getManifest().getAttributes()); return null; } }); }
private void configureJarTask(Project project, GradlePluginDevelopmentExtension extension) { Jar jarTask = (Jar) project.getTasks().findByName(JAR_TASK); List<PluginDescriptor> descriptors = new ArrayList<PluginDescriptor>(); Set<String> classList = new HashSet<String>(); PluginDescriptorCollectorAction pluginDescriptorCollector = new PluginDescriptorCollectorAction(descriptors); ClassManifestCollectorAction classManifestCollector = new ClassManifestCollectorAction(classList); PluginValidationAction pluginValidationAction = new PluginValidationAction(extension.getPlugins(), descriptors, classList); jarTask.filesMatching(PLUGIN_DESCRIPTOR_PATTERN, pluginDescriptorCollector); jarTask.filesMatching(CLASSES_PATTERN, classManifestCollector); jarTask.appendParallelSafeAction(pluginValidationAction); }
private void configureArchivesAndComponent(final Project project, final JavaPluginConvention pluginConvention) { Jar jar = project.getTasks().create(JAR_TASK_NAME, Jar.class); jar.setDescription("Assembles a jar archive containing the main classes."); jar.setGroup(BasePlugin.BUILD_GROUP); jar.from(pluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput()); ArchivePublishArtifact jarArtifact = new ArchivePublishArtifact(jar); Configuration runtimeConfiguration = project.getConfigurations().getByName(RUNTIME_CONFIGURATION_NAME); runtimeConfiguration.getArtifacts().add(jarArtifact); project.getExtensions().getByType(DefaultArtifactPublicationSet.class).addCandidate(jarArtifact); project.getComponents().add(new JavaLibrary(jarArtifact, runtimeConfiguration.getAllDependencies())); }
/** * Resolve default values to be used in manifest. * * @param project Context the default values should be resolved from. */ private void defaultAttributesUsingDetailsFrom(Project project) { Object projectGroup = project.getGroup(); String projectName = project.getName(); if (projectGroup != null) { capsuleManifest.defaultApplicationIdTo(projectGroup + "." + projectName); } Jar jarTask = (Jar) project.getTasks().getAt("jar"); capsuleManifest.defaultApplicationClassTo((String) jarTask.getManifest().getAttributes().get("Main-Class")); }
@Override public void apply(Project project) { this.project = project; jarTask = project.getTasks().create(getTaskName(), Jar.class); jarTask.setClassifier(getClassifier()); jarTask.setGroup(BasePlugin.BUILD_GROUP); project.getPluginManager().apply(BasePlugin.class); project.getArtifacts().add(Dependency.ARCHIVES_CONFIGURATION, getJarTask()); }
@Override public void apply(Project project) { project.getTasks().withType(War.class, war -> { WarArchiveClassesConvention archiveClassesConvention = new WarArchiveClassesConvention(); war.getConvention().getPlugins().put("archiveClasses", archiveClassesConvention); Jar warClassesJar = project.getTasks().create(war.getName() + "ClassesJar", Jar.class); warClassesJar.getConventionMapping().map("baseName", war::getBaseName); warClassesJar.getConventionMapping().map("appendix", war::getAppendix); warClassesJar.getConventionMapping().map("version", war::getVersion); warClassesJar.getConventionMapping().map("classifier", war::getClassifier); project.afterEvaluate(p -> { warClassesJar.setEnabled(archiveClassesConvention.isArchiveClasses()); if (archiveClassesConvention.isArchiveClasses()) { FileCollection warClasspath = war.getClasspath(); warClassesJar.from(warClasspath != null ? warClasspath.filter(File::isDirectory) : Collections.emptyList()); war.setClasspath(warClasspath == null ? null : warClasspath.filter(File::isFile).plus(project.files(warClassesJar))); } }); }); }
/** * Create task to create sources JAR * * @param sourcesJar Sources JAR task to configure * @param source Source set to add to the JAR */ @Mutate public void configureSourcesJarTask(@Path("tasks.sourcesJar") Jar sourcesJar, ProjectSourceSet source) { sourcesJar.setDescription("Assembles a jar archive containing the sources."); sourcesJar.setGroup("build"); sourcesJar.setClassifier("sources"); sourcesJar.from(source.withType(JavaSourceSet.class).stream().filter(s -> s.getParentName().equals("main")) .findAny().get().getSource()); }
/** * Create a task to create a JavaDoc JAR * * @param javadocJar Task to configure * @param javadoc JavaDoc task */ @Mutate public void configureJavadocJarTask(@Path("tasks.javadocJar") Jar javadocJar, @Path("tasks.javadoc") Javadoc javadoc) { javadocJar.setDescription("Assembles a jar archive containing the JavaDoc documentation."); javadocJar.setGroup("build"); javadocJar.setClassifier("javadoc"); javadocJar.from(javadoc); javadocJar.dependsOn(javadoc); }
/** * Configure task to create a JAR with the test classes * * @param testJar Task to configure * @param compileTestJava Java compile task for tests * @param processTestResources Java resources task for tests */ @Mutate public void configureTestJarTask(@Path("tasks.testJar") Jar testJar, @Path("tasks.compileTestJava") Task compileTestJava, @Path("tasks.processTestResources") Task processTestResources) { testJar.setDescription("Assembles a jar archive containing the unit tests.."); testJar.setGroup("build"); testJar.from(compileTestJava, processTestResources); testJar.dependsOn(compileTestJava, processTestResources); }
/** * Create task to create the GroovyDoc jar * * @param task Tasl to configure * @param groovydocTask Groovydoc task */ @Mutate public void configureGroovydocJarTask(@Path("tasks.groovydocJar") Jar task, @Path("tasks.groovydoc") Groovydoc groovydocTask) { task.setDescription("Assembles a jar archive containing the groovydoc documentation."); task.setGroup("build"); task.setClassifier("groovydoc"); task.from(groovydocTask); }
@Test public void testDefaultConfigurationAlternative() throws IOException { Project p = new TestProject(testProjectDir.getRoot()).addDockerDir().applyFlexibleProjectBuilder(); ExtensionAware ext = (ExtensionAware) p.getExtensions().getByName(AppEngineCorePlugin.APPENGINE_EXTENSION); StageFlexibleExtension stageExt = new ExtensionUtil(ext).get(AppEngineFlexiblePlugin.STAGE_EXTENSION); assertTrue(new File(testProjectDir.getRoot(), "src/main/docker").exists()); assertEquals((((Jar) p.getProperties().get("jar")).getArchivePath()), stageExt.getArtifact()); }
/** * Sets the sourcesJar for comparision with {@link #getPreviousSourcesJar()}. * Task dependency will be automatically added from this task to sourcesJar task supplied as parameter. * During comparison, the algorithm will read jar's output file using {@link Jar#getArchivePath()}. */ public void compareSourcesJar(Jar sourcesJar) { //when we compare, we can get the sources jar file via sourcesJar.archivePath this.sourcesJar = sourcesJar; //so that when we compare jars, the local sources jar is already built. this.dependsOn(sourcesJar); }
@TaskAction public void repackage() { Project project = getProject(); SpringBootPluginExtension extension = project.getExtensions() .getByType(SpringBootPluginExtension.class); ProjectLibraries libraries = getLibraries(); project.getTasks().withType(Jar.class, new RepackageAction(extension, libraries)); }
@Override public void execute(Jar jarTask) { if ("".equals(jarTask.getClassifier())) { String classifier = this.task.getClassifier(); if (classifier == null) { SpringBootPluginExtension extension = this.project.getExtensions() .getByType(SpringBootPluginExtension.class); classifier = extension.getClassifier(); this.task.setClassifier(classifier); } if (classifier != null) { setupInputOutputs(jarTask, classifier); } } }
private void setupInputOutputs(Jar jarTask, String classifier) { Logger logger = this.project.getLogger(); logger.debug("Using classifier: " + classifier + " for task " + this.task.getName()); File inputFile = jarTask.getArchivePath(); String outputName = inputFile.getName(); outputName = StringUtils.stripFilenameExtension(outputName) + "-" + classifier + "." + StringUtils.getFilenameExtension(outputName); File outputFile = new File(inputFile.getParentFile(), outputName); this.task.getInputs().file(jarTask); addLibraryDependencies(this.task); this.task.getOutputs().file(outputFile); this.task.setOutputFile(outputFile); }
@Override protected void applyOnce(Project proj) { BndManifestExtension extension = proj.getExtensions().create(BndManifestExtension.NAME, BndManifestExtension.class); proj.afterEvaluate(project -> { // find the file that the user would like us to copy to (if any) Optional<File> copyTo = Optional.ofNullable(extension.copyTo).map(proj::file); ProjectPlugin.getPlugin(project, JavaPlugin.class); Jar jarTask = (Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME); jarTask.deleteAllActions(); jarTask.getInputs().properties(jarTask.getManifest().getEffectiveManifest().getAttributes()); jarTask.getOutputs().file(jarTask.getArchivePath()); copyTo.ifPresent(jarTask.getOutputs()::file); jarTask.doLast(Errors.rethrow().wrap(unused -> { // find the location of the manifest in the output resources directory JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); SourceSet main = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); Path outputManifest = main.getOutput().getResourcesDir().toPath().resolve("META-INF/MANIFEST.MF"); // if we don't want to merge, then delete the existing manifest so that bnd doesn't merge with it if (!extension.mergeWithExisting) { Files.deleteIfExists(outputManifest); } // take the bnd action takeBndAction(project, jar -> { // write out the jar file createParents(jarTask.getArchivePath()); jar.write(jarTask.getArchivePath()); // write out the manifest to the resources output directory for the test task (and others) writeFile(outputManifest.toFile(), jar::writeManifest); // write the manifest to copyTo, if we're supposed to if (copyTo.isPresent()) { writeFile(copyTo.get(), jar::writeManifest); } }); })::accept); }); }
@Override protected FileSignature calculateState() throws Exception { Set<File> files = new LinkedHashSet<>(); for (Object o : projConfigMaven) { if (o instanceof Project) { Project project = (Project) o; Jar jar = taskFor(project); files.add(jar.getArchivePath()); files.addAll(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CONFIGURATION_NAME).resolve()); } else if (o instanceof Configuration) { Configuration config = (Configuration) o; files.addAll(config.resolve()); } else if (o instanceof String) { String mavenCoord = (String) o; Dependency dep = setupTask.getProject().getDependencies().create(mavenCoord); files.addAll(setupTask.getProject().getConfigurations() .detachedConfiguration(dep) .setDescription(mavenCoord) .setTransitive(false) .resolve()); } else { throw Unhandled.classException(o); } } return FileSignature.signAsList(files); }
private Task createJarTask(TaskContainer tasks, JarBinarySpecInternal binary) { Jar jar = tasks.create(binary.getNamingScheme().getTaskName("create"), Jar.class); jar.setDescription(String.format("Creates the binary file for %s.", binary.getNamingScheme().getDescription())); jar.from(binary.getClassesDir()); jar.from(binary.getResourcesDir()); jar.setDestinationDir(binary.getJarFile().getParentFile()); jar.setArchiveName(binary.getJarFile().getName()); return jar; }