Search in sources :

Example 1 with Task

use of org.gradle.api.Task in project hibernate-orm by hibernate.

the class HibernatePlugin method applyEnhancement.

private void applyEnhancement(final Project project, final HibernateExtension hibernateExtension) {
    if (!hibernateExtension.enhance.shouldApply()) {
        project.getLogger().warn("Skipping Hibernate bytecode enhancement since no feature is enabled");
        return;
    }
    for (final SourceSet sourceSet : hibernateExtension.getSourceSets()) {
        project.getLogger().debug("Applying Hibernate enhancement action to SourceSet.{}", sourceSet.getName());
        final Task compileTask = project.getTasks().findByName(sourceSet.getCompileJavaTaskName());
        compileTask.doLast(new Action<Task>() {

            @Override
            public void execute(Task task) {
                project.getLogger().debug("Starting Hibernate enhancement on SourceSet.{}", sourceSet.getName());
                final ClassLoader classLoader = toClassLoader(sourceSet.getRuntimeClasspath());
                EnhancementContext enhancementContext = new DefaultEnhancementContext() {

                    @Override
                    public ClassLoader getLoadingClassLoader() {
                        return classLoader;
                    }

                    @Override
                    public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
                        return hibernateExtension.enhance.getEnableAssociationManagement();
                    }

                    @Override
                    public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableDirtyTracking();
                    }

                    @Override
                    public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableLazyInitialization();
                    }

                    @Override
                    public boolean isLazyLoadable(UnloadedField field) {
                        return hibernateExtension.enhance.getEnableLazyInitialization();
                    }

                    @Override
                    public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
                        return hibernateExtension.enhance.getEnableExtendedEnhancement();
                    }
                };
                if (hibernateExtension.enhance.getEnableExtendedEnhancement()) {
                    logger.warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
                }
                final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
                final FileTree fileTree = project.fileTree(sourceSet.getOutput().getClassesDir());
                for (File file : fileTree) {
                    if (!file.getName().endsWith(".class")) {
                        continue;
                    }
                    final byte[] enhancedBytecode = doEnhancement(sourceSet.getOutput().getClassesDir(), file, enhancer);
                    if (enhancedBytecode != null) {
                        writeOutEnhancedClass(enhancedBytecode, file);
                        logger.info("Successfully enhanced class [" + file + "]");
                    } else {
                        logger.info("Skipping class [" + file.getAbsolutePath() + "], not an entity nor embeddable");
                    }
                }
            }
        });
    }
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) URLClassLoader(java.net.URLClassLoader) FileTree(org.gradle.api.file.FileTree) File(java.io.File) UnloadedField(org.hibernate.bytecode.enhance.spi.UnloadedField) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext)

Example 2 with Task

use of org.gradle.api.Task in project atlas by alibaba.

the class PublishHooker method hookPublish.

public void hookPublish() {
    project.getTasks().whenTaskAdded(new Action<Task>() {

        @Override
        public void execute(Task task) {
            if ("publishMavenPublicationToMavenRepository".equals(task.getName())) {
                publishTask = (PublishToMavenRepository) task;
                task.setEnabled(false);
                if (null != hookTask) {
                    hookTask.setRepository(publishTask.getRepository());
                    hookTask.setPublication(publishTask.getPublication());
                }
            }
        }
    });
    final TaskContainer tasks = project.getTasks();
    Task publishLifecycleTask = tasks.getByName(PublishingPlugin.PUBLISH_LIFECYCLE_TASK_NAME);
    String taskName = "publishMavenPublicationToMavenRepositoryHook";
    hookTask = tasks.create(taskName, PublishToMavenRepositoryHook.class, new Action<PublishToMavenRepositoryHook>() {

        public void execute(PublishToMavenRepositoryHook publishTask) {
            publishTask.setGroup(PublishingPlugin.PUBLISH_TASK_GROUP);
        }
    });
    publishLifecycleTask.dependsOn(taskName);
}
Also used : Task(org.gradle.api.Task) Action(org.gradle.api.Action) TaskContainer(org.gradle.api.tasks.TaskContainer) PublishToMavenRepository(org.gradle.api.publish.maven.tasks.PublishToMavenRepository) PublishToMavenRepositoryHook(com.taobao.android.builder.tasks.maven.PublishToMavenRepositoryHook)

Example 3 with Task

use of org.gradle.api.Task in project atlas by alibaba.

the class MtlTaskFactoryImpl method createTask.

@Override
public Task createTask(VariantContext variantContext, BaseVariantOutputData vod, Class<? extends MtlBaseTaskAction> baseTaskAction) {
    if (null == baseTaskAction) {
        return null;
    }
    try {
        MtlBaseTaskAction mtlBaseTaskAction = getConstructor(baseTaskAction, variantContext.getClass()).newInstance(variantContext, vod);
        Task task = variantContext.getProject().getTasks().create(mtlBaseTaskAction.getName(), mtlBaseTaskAction.getType());
        mtlBaseTaskAction.execute(task);
        return task;
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Task(org.gradle.api.Task) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with Task

use of org.gradle.api.Task in project intellij-community by JetBrains.

the class ModuleExtendedModelBuilderImpl method buildAll.

@Nullable
@Override
public Object buildAll(String modelName, Project project) {
    final String moduleName = project.getName();
    final String moduleGroup = project.getGroup().toString();
    final String moduleVersion = project.getVersion().toString();
    final File buildDir = project.getBuildDir();
    String javaSourceCompatibility = null;
    for (Task task : project.getTasks()) {
        if (task instanceof JavaCompile) {
            JavaCompile javaCompile = (JavaCompile) task;
            javaSourceCompatibility = javaCompile.getSourceCompatibility();
            if (task.getName().equals("compileJava"))
                break;
        }
    }
    final ModuleExtendedModelImpl moduleVersionModel = new ModuleExtendedModelImpl(moduleName, moduleGroup, moduleVersion, buildDir, javaSourceCompatibility);
    final List<File> artifacts = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Jar) {
            Jar jar = (Jar) task;
            artifacts.add(jar.getArchivePath());
        }
    }
    moduleVersionModel.setArtifacts(artifacts);
    final Set<String> sourceDirectories = new HashSet<String>();
    final Set<String> testDirectories = new HashSet<String>();
    final Set<String> resourceDirectories = new HashSet<String>();
    final Set<String> testResourceDirectories = new HashSet<String>();
    final List<File> testClassesDirs = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Test) {
            Test test = (Test) task;
            testClassesDirs.add(test.getTestClassesDir());
            if (test.hasProperty(TEST_SRC_DIRS_PROPERTY)) {
                Object testSrcDirs = test.property(TEST_SRC_DIRS_PROPERTY);
                if (testSrcDirs instanceof Iterable) {
                    for (Object dir : Iterable.class.cast(testSrcDirs)) {
                        addFilePath(testDirectories, dir);
                    }
                }
            }
        }
    }
    IdeaCompilerOutputImpl compilerOutput = new IdeaCompilerOutputImpl();
    if (project.hasProperty(SOURCE_SETS_PROPERTY)) {
        Object sourceSets = project.property(SOURCE_SETS_PROPERTY);
        if (sourceSets instanceof SourceSetContainer) {
            SourceSetContainer sourceSetContainer = (SourceSetContainer) sourceSets;
            for (SourceSet sourceSet : sourceSetContainer) {
                SourceSetOutput output = sourceSet.getOutput();
                if (SourceSet.TEST_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setTestClassesDir(output.getClassesDir());
                    compilerOutput.setTestResourcesDir(output.getResourcesDir());
                }
                if (SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setMainClassesDir(output.getClassesDir());
                    compilerOutput.setMainResourcesDir(output.getResourcesDir());
                }
                for (File javaSrcDir : sourceSet.getAllJava().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testDirectories : sourceDirectories, javaSrcDir);
                }
                for (File resourcesSrcDir : sourceSet.getResources().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testResourceDirectories : resourceDirectories, resourcesSrcDir);
                }
            }
        }
    }
    File projectDir = project.getProjectDir();
    IdeaContentRootImpl contentRoot = new IdeaContentRootImpl(projectDir);
    final Set<String> ideaSourceDirectories = new HashSet<String>();
    final Set<String> ideaTestDirectories = new HashSet<String>();
    final Set<String> ideaGeneratedDirectories = new HashSet<String>();
    final Set<File> excludeDirectories = new HashSet<File>();
    enrichDataFromIdeaPlugin(project, excludeDirectories, ideaSourceDirectories, ideaTestDirectories, ideaGeneratedDirectories);
    if (ideaSourceDirectories.isEmpty()) {
        sourceDirectories.clear();
        resourceDirectories.clear();
    }
    if (ideaTestDirectories.isEmpty()) {
        testDirectories.clear();
        testResourceDirectories.clear();
    }
    ideaSourceDirectories.removeAll(resourceDirectories);
    sourceDirectories.removeAll(ideaTestDirectories);
    sourceDirectories.addAll(ideaSourceDirectories);
    ideaTestDirectories.removeAll(testResourceDirectories);
    testDirectories.addAll(ideaTestDirectories);
    // ensure disjoint directories with different type
    resourceDirectories.removeAll(sourceDirectories);
    testDirectories.removeAll(sourceDirectories);
    testResourceDirectories.removeAll(testDirectories);
    for (String javaDir : sourceDirectories) {
        contentRoot.addSourceDirectory(new IdeaSourceDirectoryImpl(new File(javaDir), ideaGeneratedDirectories.contains(javaDir)));
    }
    for (String testDir : testDirectories) {
        contentRoot.addTestDirectory(new IdeaSourceDirectoryImpl(new File(testDir), ideaGeneratedDirectories.contains(testDir)));
    }
    for (String resourceDir : resourceDirectories) {
        contentRoot.addResourceDirectory(new IdeaSourceDirectoryImpl(new File(resourceDir)));
    }
    for (String testResourceDir : testResourceDirectories) {
        contentRoot.addTestResourceDirectory(new IdeaSourceDirectoryImpl(new File(testResourceDir)));
    }
    for (File excludeDir : excludeDirectories) {
        contentRoot.addExcludeDirectory(excludeDir);
    }
    moduleVersionModel.setContentRoots(Collections.<ExtIdeaContentRoot>singleton(contentRoot));
    moduleVersionModel.setCompilerOutput(compilerOutput);
    ConfigurationContainer configurations = project.getConfigurations();
    SortedMap<String, Configuration> configurationsByName = configurations.getAsMap();
    Map<String, Set<File>> artifactsByConfiguration = new HashMap<String, Set<File>>();
    for (Map.Entry<String, Configuration> configurationEntry : configurationsByName.entrySet()) {
        Set<File> files = configurationEntry.getValue().getAllArtifacts().getFiles().getFiles();
        artifactsByConfiguration.put(configurationEntry.getKey(), files);
    }
    moduleVersionModel.setArtifactsByConfiguration(artifactsByConfiguration);
    return moduleVersionModel;
}
Also used : Task(org.gradle.api.Task) SourceSet(org.gradle.api.tasks.SourceSet) Configuration(org.gradle.api.artifacts.Configuration) IdeaContentRootImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaContentRootImpl) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) SourceSetOutput(org.gradle.api.tasks.SourceSetOutput) ModuleExtendedModelImpl(org.jetbrains.plugins.gradle.tooling.internal.ModuleExtendedModelImpl) SourceSet(org.gradle.api.tasks.SourceSet) Test(org.gradle.api.tasks.testing.Test) IdeaSourceDirectoryImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaSourceDirectoryImpl) IdeaCompilerOutputImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaCompilerOutputImpl) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Task

use of org.gradle.api.Task in project gradle by gradle.

the class ProjectConverter method addTasks.

/**
     * Adds the tasks from the project to the GradleProject.
     *
     * @param project the source parent project. Where we get the sub projects from.
     * @param projectView the destination of the tasks from project.
     */
private void addTasks(Project project, ProjectView projectView) {
    List<String> defaultTasks = project.getDefaultTasks();
    for (Task task : taskLister.listProjectTasks(project)) {
        String taskName = task.getName();
        boolean isDefault = defaultTasks.contains(taskName);
        projectView.createTask(taskName, task.getDescription(), isDefault);
    }
}
Also used : Task(org.gradle.api.Task)

Aggregations

Task (org.gradle.api.Task)222 Project (org.gradle.api.Project)66 File (java.io.File)54 Action (org.gradle.api.Action)31 ArrayList (java.util.ArrayList)27 Plugin (org.gradle.api.Plugin)27 Configuration (org.gradle.api.artifacts.Configuration)27 Test (org.junit.Test)23 List (java.util.List)22 TaskProvider (org.gradle.api.tasks.TaskProvider)22 DefaultTask (org.gradle.api.DefaultTask)21 IOException (java.io.IOException)20 Set (java.util.Set)18 SourceSet (org.gradle.api.tasks.SourceSet)18 Map (java.util.Map)17 GradleException (org.gradle.api.GradleException)17 FileCollection (org.gradle.api.file.FileCollection)16 Inject (javax.inject.Inject)15 JavaPlugin (org.gradle.api.plugins.JavaPlugin)15 LifecycleBasePlugin (org.gradle.language.base.plugins.LifecycleBasePlugin)15