Passing xml style custom code to gradle plugin

Is there a way to tell the gradle idea plugin to use custom code XML when creating project files?

I can always copy xml to "~ / Library / Preferences / IntelliJIdea13 / codestyles" and then change the code style after importing the project, but I would like the gradle plugin to do this automatically for me as part of its generation.

Thank!

+4
source share
2 answers

Just in case someone is trying to do this, I managed to solve the problem using plugins to modify the ipr project file before it is written to disk. Basically, adding the following to your build.gradle:

idea {
  project {
    ipr {
      withXml { provider -> addCodeStyle(provider) }
    }
  }
}

  def addCodeStyle(provider) {
      def project = provider.asNode()
      project.appendNode('component', [name: 'ProjectCodeStyleSettingsManager'])

      def codeStyleNode = findComponent(project, 'ProjectCodeStyleSettingsManager')
      codeStyleNode.appendNode('option', [name: 'USE_PER_PROJECT_SETTINGS', value: 'true'])
      def projectSettingsNode = codeStyleNode.appendNode('option', [name: 'PER_PROJECT_SETTINGS']).appendNode('value')    

      def codeStyleUrl = "fileUrl".toURL()

      //If you want to read from a file you could do new File(path).text
      def codeStyleXml = new XmlParser().parseText(codeStyleUrl.text)
      codeStyleXml.children().each { option ->
          projectSettingsNode.append(option)
      }
  }

, xml :

<?xml version="1.0" encoding="UTF-8"?>
<code_scheme name="X">
  <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="6" />
  <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="9" />

, , IntelliJ.

+3

IDEA 2016.2.5. , ( , ). ( ):

task copyCodeStyle(type: Copy) {
    from 'codeStyleSettings.xml'
    into '.idea'
}
tasks.idea.dependsOn copyCodeStyle

codeStyleSettings.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectCodeStyleSettingsManager">
    <option name="PER_PROJECT_SETTINGS">
      <value>
        <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="8" />
        <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="300" />
+1

All Articles