How to add source folder to grails application

2019-02-25 10:12发布

问题:

I use STS for developing Grails application, and I need to use there a bunch of classes generated by wsimport utility. In order to not to mix my source with autogenerated source, I want to add separate directory and put there generated classes, like this:

grails-project  
 |  
 |-- .classpath  
 |-- .groovy  
 |-- .project  
 |-- .settings  
 |-- application.properties  
 |-- grails-app  
 |-- lib  
 |-- scripts  
 |-- src
 |   |-- groovy
 |   |-- java
 |   `-- wsimport     <- where I want to make additional source folder
 |-- target  
 |-- target-eclipse  
 |-- test  
 `-- web-app

I can add new classpath entry to .classpath file and STS will recognize sources, but what do I do to Grails? Do I need to specify it in some config file or something?

回答1:

The answer is here:

http://ofps.oreilly.com/titles/9781449323936/chapter_configuration.html

To summarize, you can use configuration like this:

extraSrcDirs = ["$basedir/src/extra1", "$basedir/src/extra2", ...]

eventCompileStart = {
   for (String path in extraSrcDirs) {
      projectCompiler.srcDirectories << path
   }
   copyResources buildSettings.resourcesDir
}

eventCreateWarStart = { warName, stagingDir ->
   copyResources "$stagingDir/WEB-INF/classes"
}

private copyResources(destination) {
   ant.copy(todir: destination,
            failonerror: false,
            preservelastmodified: true) {
      for (String path in extraSrcDirs) {
         fileset(dir: path) {
            exclude(name: '*.groovy')
            exclude(name: '*.java')
         }
      }
   }
}

This will let the grails compiler know about the extra source folders, but I don't think it is enough for STS to know about the source folders. For that, you will have to continue updating the project's .classpath.