Search in sources :

Example 36 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 37 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 38 with Task

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

the class MergeResAwbsConfigAction method execute.

@Override
public void execute(MtlParallelTask parallelTask) {
    super.execute(parallelTask);
    AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(parallelTask.getVariantName());
    if (null == androidDependencyTree) {
        return;
    }
    List<DefaultTask> tasks = new ArrayList<DefaultTask>();
    for (final AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
        MergeAwbResourceConfigAction mergeAwbResourceConfigAction = new MergeAwbResourceConfigAction(appVariantContext, baseVariantOutputData, awbBundle);
        final MergeResources mergeTask = TaskCreater.create(appVariantContext.getProject(), mergeAwbResourceConfigAction.getName(), mergeAwbResourceConfigAction.getType());
        mergeAwbResourceConfigAction.execute(mergeTask);
        tasks.add(mergeTask);
        mergeTask.doLast(new Action<Task>() {

            @Override
            public void execute(Task task) {
                File publicRes = new File(awbBundle.getResFolder(), "values/public.xml");
                if (publicRes.exists()) {
                    try {
                        FileUtils.copyFile(publicRes, new File(mergeTask.getOutputDir(), "values/public.xml"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    parallelTask.parallelTask = tasks;
    parallelTask.uniqueTaskName = getName();
    parallelTask.concurrent = true;
}
Also used : MergeResources(com.android.build.gradle.tasks.MergeResources) MtlParallelTask(com.taobao.android.builder.tasks.app.MtlParallelTask) Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) MergeAwbResourceConfigAction(com.taobao.android.builder.tasks.bundle.MergeAwbResourceConfigAction) ArrayList(java.util.ArrayList) DefaultTask(org.gradle.api.DefaultTask) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) IOException(java.io.IOException) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File)

Example 39 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 40 with Task

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

the class MtlTaskInjector method injectTasks.

public void injectTasks(List<MtlTaskContext> mtlTaskContexts, AndroidBuilder androidBuilder) {
    List<BaseVariantOutputData> baseVariantOutputDataList = variantContext.getVariantOutputData();
    for (final BaseVariantOutputData vod : baseVariantOutputDataList) {
        //TODO 如果中间步骤没有生成task怎么办
        List<Task> beforeTasks = new ArrayList<Task>();
        for (MtlTaskContext mtlTaskContext : mtlTaskContexts) {
            //获取task实例
            List<Task> tasks = new ArrayList<Task>();
            if (mtlTaskContext.isSystemTask()) {
                if (null != mtlTaskContext.getSysTask()) {
                    tasks.add(mtlTaskContext.getSysTask());
                } else if (null != mtlTaskContext.getSysTaskClazz()) {
                    Class taskClazz = mtlTaskContext.getSysTaskClazz();
                    tasks.addAll(findTask(taskClazz, vod.variantData.getName()));
                } else {
                    Task task = project.getTasks().findByName(vod.getScope().getTaskName(mtlTaskContext.getTaskName()));
                    if (null != task) {
                        tasks.add(task);
                    }
                }
            } else {
                Task task = mtlTaskFactory.createTask(variantContext, vod, mtlTaskContext.getTaskActionClazz());
                if (null != task) {
                    if (null != androidBuilder && task instanceof BaseTask) {
                        ((BaseTask) task).setAndroidBuilder(androidBuilder);
                    }
                    tasks.add(task);
                }
            }
            if (tasks.isEmpty()) {
                //                    throw new RuntimeException("task is not found " + mtlTaskContext);
                project.getLogger().info("task is not found " + vod.getFullName() + mtlTaskContext);
            } else {
                if (!beforeTasks.isEmpty()) {
                    for (Task task : tasks) {
                        project.getLogger().debug("[MtlTaskInjector]" + vod.getFullName() + ":" + task + "->" + StringUtils.join(beforeTasks.toArray()));
                        for (Task before : beforeTasks) {
                            project.getLogger().info("[tasks] set task " + task.getProject().getName() + ":" + task.getName() + "->" + before.getProject().getName() + ":" + before.getName());
                            task.dependsOn(before);
                        }
                    }
                }
                beforeTasks.clear();
                beforeTasks.addAll(tasks);
                if (null != mtlTaskContext.getTaskFilter()) {
                    List<Task> beforeTasks2 = mtlTaskContext.getTaskFilter().getBeforeTasks(project, vod);
                    for (Task task : tasks) {
                        project.getLogger().debug("[MtlTaskInjector]" + vod.getFullName() + ":" + task + "->" + StringUtils.join(beforeTasks2.toArray()));
                        for (Task before : beforeTasks2) {
                            project.getLogger().info("[tasks] set task " + task.getProject().getName() + ":" + task.getName() + "->" + before.getProject().getName() + ":" + before.getName());
                            task.dependsOn(before);
                        }
                    }
                }
            }
        }
    }
}
Also used : BaseTask(com.android.build.gradle.internal.tasks.BaseTask) Task(org.gradle.api.Task) DefaultAndroidTask(com.android.build.gradle.internal.tasks.DefaultAndroidTask) ArrayList(java.util.ArrayList) BaseTask(com.android.build.gradle.internal.tasks.BaseTask) BaseVariantOutputData(com.android.build.gradle.internal.variant.BaseVariantOutputData)

Aggregations

Task (org.gradle.api.Task)116 Test (org.junit.Test)21 File (java.io.File)18 DefaultTask (org.gradle.api.DefaultTask)16 Expectations (org.jmock.Expectations)14 Project (org.gradle.api.Project)12 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)6 Callable (java.util.concurrent.Callable)6 Action (org.gradle.api.Action)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 DefaultAndroidTask (com.android.build.gradle.internal.tasks.DefaultAndroidTask)4 List (java.util.List)4 Set (java.util.Set)4 InvalidUserDataException (org.gradle.api.InvalidUserDataException)4 TaskInternal (org.gradle.api.internal.TaskInternal)4 Spec (org.gradle.api.specs.Spec)4 TaskContainer (org.gradle.api.tasks.TaskContainer)4 TaskDependency (org.gradle.api.tasks.TaskDependency)4