神刀安全网

Gradle Goodness: Add Spring Facet To IntelliJ IDEA Module

Gradle Goodness: Add Spring Facet To IntelliJ IDEA Module

How to create IntelliJ project files with Gradle and adding a Spring facet to automatically search for Spring configuration files.

Mar. 26, 16 · DevOps Zone

Comment ( 0 )

Save

{{ articles[0].views | formatCount}} Views

  • Edit
  • {{ articles[0].isLocked ? ‘Enable’ : ‘Disable’ }} comments
  • {{ articles[0].isLimited ? ‘Remove comment limits’ : ‘Enable moderated comments’ }}

The DevOps Zone is brought to you in partnership with New Relic .  Download 5 Unsung Tools of DevOps to see which tools make the cut and why.

To create IntelliJ IDEA project files with Gradle we first need to apply the idea plugin. We can then further customise the created files. In this blog post we will add a Spring facet to the generated module file. By adding the Spring facet IntelliJ IDEA can automatically search for Spring configuration files. We can then use the  Spring view to see which beans are configured in our project.

In the following example build file we use the withXml hook. This method is invoked just before the XML file is generated. We get an argument of type XmlProvider . From the  XmlProvider we can access the XML as  org.w3c.dom.ElementStringBuilder or  groovy.util.Node . We use Node to alter the XML. We check if a  FacetManager component is available. We need this to add a facet of type Spring.

// File: build.gradle apply plugin: 'groovy' apply plugin: 'idea'  idea {     module {         iml {             withXml {                  // Get root of module as groovy.util.Node.                 def moduleRoot = it.asNode()                  // Find if component with name 'FacetManager'                 // is already set.                 def facetManager = moduleRoot.component.find { component -> component.'@name' == 'FacetManager'}                 if (!facetManager) {                     // Create new component with name 'FacetManager'                     facetManager = moduleRoot.appendNode('component', [name: 'FacetManager'])                 }                  // Find Spring facet it might already be there.                 def springFacet = facetManager.facet.find { facet -> facet.'@type' == 'Spring' && facet.'@name' == 'Spring' }                 if (!springFacet) {                     // If not set create new facet node with name 'Spring'                     // and type 'Spring' and apply a default configuration.                     springFacet = facetManager.appendNode('facet', [type: 'Spring', name: 'Spring'])                     springFacet.appendNode('configuration')                 }             }         }     } }

Written with Gradle 2.12 and IntelliJ IDEA 15.

The DevOps Zone is brought to you in partnership with New Relic .  Read about where DevOps started and how the adoption rate went from tiny startups to giant Fortune 500 enterprises.

Topics:

gradle, intellij, spring

Published at DZone with permission of Hubert Klein Ikkink , DZone MVB .

Opinions expressed by DZone contributors are their own.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Gradle Goodness: Add Spring Facet To IntelliJ IDEA Module

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮