Java Code Examples for org.gradle.jvm.tasks.Jar

The following examples show how to use org.gradle.jvm.tasks.Jar. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: quarkus   Source File: QuarkusPluginExtension.java    License: Apache License 2.0 5 votes vote down vote up
public Path appJarOrClasses() {
    final Jar jarTask = (Jar) project.getTasks().findByName(JavaPlugin.JAR_TASK_NAME);
    if (jarTask == null) {
        throw new RuntimeException("Failed to locate task 'jar' in the project.");
    }
    final Provider<RegularFile> jarProvider = jarTask.getArchiveFile();
    Path classesDir = null;
    if (jarProvider.isPresent()) {
        final File f = jarProvider.get().getAsFile();
        if (f.exists()) {
            classesDir = f.toPath();
        }
    }
    if (classesDir == null) {
        final Convention convention = project.getConvention();
        JavaPluginConvention javaConvention = convention.findPlugin(JavaPluginConvention.class);
        if (javaConvention != null) {
            final SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
            final String classesPath = QuarkusGradleUtils.getClassesDir(mainSourceSet, jarTask.getTemporaryDir());
            if (classesPath != null) {
                classesDir = Paths.get(classesPath);
            }
        }
    }
    if (classesDir == null) {
        throw new RuntimeException("Failed to locate project's classes directory");
    }
    return classesDir;
}
 
Example 2
Source Project: javaide   Source File: BasePlugin.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void configureArchiveDefaults(final Project project, final BasePluginConvention pluginConvention) {
    project.getTasks().withType(AbstractArchiveTask.class, new Action<AbstractArchiveTask>() {
        public void execute(AbstractArchiveTask task) {
            ConventionMapping taskConventionMapping = task.getConventionMapping();

            Callable<File> destinationDir;
            if (task instanceof Jar) {
                destinationDir = new Callable<File>() {
                    public File call() throws Exception {
                        return pluginConvention.getLibsDir();
                    }
                };
            } else {
                destinationDir = new Callable<File>() {
                    public File call() throws Exception {
                        return pluginConvention.getDistsDir();
                    }
                };
            }
            taskConventionMapping.map("destinationDir", destinationDir);

            taskConventionMapping.map("version", new Callable<String>() {
                public String call() throws Exception {
                    return project.getVersion() == Project.DEFAULT_VERSION ? null : project.getVersion().toString();
                }
            });

            taskConventionMapping.map("baseName", new Callable<String>() {
                public String call() throws Exception {
                    return pluginConvention.getArchivesBaseName();
                }
            });
        }
    });
}
 
Example 3
Source Project: shipkit   Source File: JavadocPlugin.java    License: MIT License 5 votes vote down vote up
private CopyTasks createCopyJavadocToStageTasks(Project project) {
    Set<Copy> copyToVersionTasks = new HashSet<>();
    Set<Copy> copyToCurrentTasks = new HashSet<>();

    Set<Task> javadocJarSet = project.getTasksByName("javadocJar", true);
    if (javadocJarSet.isEmpty()) {
        LOGGER.warn("Not found any 'javadocJar' task. You probably applied 'org.shipkit.javadoc' plugin before 'org.shipkit.java'. " +
            "Please apply 'org.shipkit.java' plugin first!");
    }
    javadocJarSet.stream()
        .map(task -> (Jar) task)
        .forEach(javadocJarTask -> {
            Copy copyToVersionTask = TaskMaker.task(javadocJarTask.getProject(), COPY_JAVADOC_TO_STAGE_VERSION_DIR_TASK, Copy.class, copyTask -> {
                copyTask.setDescription("Extracts contents of javadoc jar to the staging /version directory");
                copyTask.dependsOn(javadocJarTask);
                copyTask.from(project.zipTree(javadocJarTask.getArchivePath()));
                // TODO how? : note that we need to use Closure/Callable because 'baseName' can be set by user later
                copyTask.into(getJavadocStageDir(project) + "/" + javadocJarTask.getBaseName() + "/" + project.getVersion());
            });
            copyToVersionTasks.add(copyToVersionTask);

            Copy copyToCurrentTask = TaskMaker.task(javadocJarTask.getProject(), COPY_JAVADOC_TO_STAGE_CURRENT_DIR_TASK, Copy.class, copyTask -> {
                copyTask.setDescription("Extracts contents of javadoc jar to the staging /current directory");
                copyTask.dependsOn(copyToVersionTask);
                copyTask.from(copyToVersionTask.getDestinationDir());
                // TODO how? : note that we need to use Closure/Callable because 'baseName' can be set by user later
                copyTask.into(getJavadocStageDir(project) + "/" + javadocJarTask.getBaseName() + "/current");
            });
            copyToCurrentTasks.add(copyToCurrentTask);
        });
    return new CopyTasks(copyToVersionTasks, copyToCurrentTasks);
}
 
Example 4
Source Project: saros   Source File: JarConfigurator.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void createBundleJar(File manifestFile) {
  Jar jarTask = (Jar) project.getTasks().findByName(JAR_TASK_NAME);

  if (jarTask == null)
    throw new GradleException("Unable to find the gradle task: " + JAR_TASK_NAME);

  jarTask.manifest((Manifest mf) -> mf.from(manifestFile));
  SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
  jarTask.from(sourceSets.getByName(JAVA_MAIN_SOURCE_SET_NAME).getOutput());
  jarTask.into(
      JAR_LIB_DESTINATION,
      (CopySpec cs) -> cs.from(project.getConfigurations().getByName(RELEASE_CONFIG_NAME)));
}
 
Example 5
Source Project: quarkus   Source File: AppModelGradleResolver.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AppModel resolveModel(AppArtifact appArtifact) throws AppModelResolverException {
    AppModel.Builder appBuilder = new AppModel.Builder();
    if (appModel != null && appModel.getAppArtifact().equals(appArtifact)) {
        return appModel;
    }
    final List<Dependency> directExtensionDeps = new ArrayList<>();

    // collect enforced platforms
    final Configuration impl = project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME);
    for (Dependency d : impl.getAllDependencies()) {
        if (!(d instanceof ModuleDependency)) {
            continue;
        }
        final ModuleDependency module = (ModuleDependency) d;
        final Category category = module.getAttributes().getAttribute(Category.CATEGORY_ATTRIBUTE);
        if (category != null && Category.ENFORCED_PLATFORM.equals(category.getName())) {
            directExtensionDeps.add(d);
        }
    }

    final List<AppDependency> userDeps = new ArrayList<>();
    Map<AppArtifactKey, AppDependency> versionMap = new HashMap<>();
    Map<ModuleIdentifier, ModuleVersionIdentifier> userModules = new HashMap<>();

    final String classpathConfigName = launchMode == LaunchMode.TEST ? JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME
            : JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME;

    collectDependencies(project.getConfigurations().getByName(classpathConfigName),
            appBuilder, directExtensionDeps, userDeps,
            versionMap, userModules);

    if (launchMode == LaunchMode.DEVELOPMENT) {
        collectDependencies(project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME),
                appBuilder, directExtensionDeps, userDeps,
                versionMap, userModules);
    }

    final List<AppDependency> deploymentDeps = new ArrayList<>();
    final List<AppDependency> fullDeploymentDeps = new ArrayList<>(userDeps);
    if (!directExtensionDeps.isEmpty()) {
        final Configuration deploymentConfig = project.getConfigurations()
                .detachedConfiguration(directExtensionDeps.toArray(new Dependency[0]));
        final ResolvedConfiguration rc = deploymentConfig.getResolvedConfiguration();
        for (ResolvedArtifact a : rc.getResolvedArtifacts()) {
            final ModuleVersionIdentifier userVersion = userModules.get(getModuleId(a));
            if (userVersion != null || !isDependency(a)) {
                continue;
            }
            final AppDependency dependency = toAppDependency(a);
            fullDeploymentDeps.add(dependency);
            if (!userDeps.contains(dependency)) {
                AppDependency deploymentDep = alignVersion(dependency, versionMap);
                deploymentDeps.add(deploymentDep);
            }
        }
    }

    if (!appArtifact.isResolved()) {
        final Jar jarTask = (Jar) project.getTasks().findByName(JavaPlugin.JAR_TASK_NAME);
        if (jarTask == null) {
            throw new AppModelResolverException("Failed to locate task 'jar' in the project.");
        }
        if (jarTask.getDidWork()) {
            final Provider<RegularFile> jarProvider = jarTask.getArchiveFile();
            Path classesDir = null;
            if (jarProvider.isPresent()) {
                final File f = jarProvider.get().getAsFile();
                if (f.exists()) {
                    classesDir = f.toPath();
                }
            }
            if (classesDir == null) {
                throw new AppModelResolverException("Failed to locate classes directory for " + appArtifact);
            }
            appArtifact.setPaths(PathsCollection.of(classesDir));
        } else {
            final Convention convention = project.getConvention();
            JavaPluginConvention javaConvention = convention.findPlugin(JavaPluginConvention.class);
            if (javaConvention != null) {
                final SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
                PathsCollection.Builder paths = PathsCollection.builder();
                mainSourceSet.getOutput().filter(s -> s.exists()).forEach(f -> {
                    paths.add(f.toPath());
                });
                for (File resourcesDir : mainSourceSet.getResources().getSourceDirectories()) {
                    if (resourcesDir.exists()) {
                        paths.add(resourcesDir.toPath());
                    }
                }
                appArtifact.setPaths(paths.build());
            }
        }
    }

    appBuilder.addRuntimeDeps(userDeps)
            .addFullDeploymentDeps(fullDeploymentDeps)
            .addDeploymentDeps(deploymentDeps)
            .setAppArtifact(appArtifact);
    return this.appModel = appBuilder.build();
}