Gradle 的好处就不多说了, 毕竟已经占领了 Android 的构建编译..

添加Intellij PluginGradle 的支持其实和 Android 差不多, 需要添加官方的插件支持.

首先你要做的是, 在你 Intellij plugin 项目的根目录里执行命令 gradle init来初始化一个 gradle 工程

执行上述命令之后,当前目录下就会创建一堆 gradle 相关的文件夹和文件, 这个时候你只需要关注 build.gradle即可.

接下来我们就开始修改 build.gradle文件,让它能够支持构建 intellij 插件.

  1. 添加 intellij build plugins 仓库地址:https://plugins.gradle.org/plugin/org.jetbrains.intellij 官方推荐了两种添加方式, 这里我们采用第二种
plugins {
  id "org.jetbrains.intellij" version "0.1.10"
}

2.使用 intellij idea 的插件

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'

3.设置运行插件的 intellij 版本以及沙箱地址

intellij {
    version = 'IU-163.7342.3' //调试我们插件的版本
    sandboxDirectory = project.rootDir.canonicalPath + "/.sandbox" //插件生成的临时文件的地址
}

完成以上操作基本上, 我们需要用 Idea 来重新以 gradle 的工程来导入我们的项目,这样就可以支持 gradle 啦. 如图所示:pic

由于 Gradle 支持缩写, 所以我们在命令行执行 ./gradlew rI就可以直接唤起调试的 Intellij Idea 啦.

下面附上我的 build.gralde 文件

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'darin' at '11/4/16 10:39 AM' with Gradle 2.12
 *
 * This generated file contains a commented-out sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/2.12/userguide/tutorial_java_projects.html
 */


plugins {
    id 'org.jetbrains.intellij' version "0.1.10"
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'


// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

intellij {
    version = 'IU-163.7342.3'
    plugins = ['JavaScriptLanguage', 'CSS']
    sandboxDirectory = project.rootDir.canonicalPath + "/.sandbox"
}

sourceSets {
    main.java.srcDirs = ['src', 'gen']
    main.resources.srcDir 'resources'

    test.java.srcDir 'test/src'
    test.resources.srcDir 'test/data'
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile fileTree(dir: 'lib', include: ['*.jar'])
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.18'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'

    compile 'com.google.zxing:core:3.2.1'
    compile 'com.google.zxing:javase:2.2'
}

问题收集:

  1. 如果需要看插件打印的 log, 可以点击图片里箭头指向的地方:
    pic