weixin_39533432
weixin_39533432
2020-12-09 04:12

Selective compilation

Hi, I have a single project (Gradle war) with the following Java sources: src/main/java/com/project/client src/main/java/com/project/shared src/main/java/com/project/server

where I want to launch Gwt compilation in the client and share packages ONLY and javac compile for the server package. How do I tell this plugin to ignore changes in the server package (or at least do only javac compilation there)?

该提问来源于开源项目:steffenschaefer/gwt-gradle-plugin

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39533432 weixin_39533432 5月前

    Stefen, Looking at this issue:

    https://github.com/steffenschaefer/gwt-gradle-plugin/issues/23

    This seems possible, can you please post an example (or update the docs)? Thanks.

    点赞 评论 复制链接分享
  • weixin_39765840 weixin_39765840 5月前

    Ideally you would refactor your project into a multi module project so that you actually have a client, shared and server project each with its own build file. This would also separate the classpath so your GWT libraries do not interference with server libraries anymore.

    I guess that would be a good additional example.

    点赞 评论 复制链接分享
  • weixin_39533432 weixin_39533432 5月前

    Yeah .. I figured that too, but I do not want the plug-in to force me to do that. I added a comment to the issue, which seems to indicate a easier way of doing it, but expect Steffen to comment too.

    On Wed, May 14, 2014 at 8:51 AM, Jens Nehlmeier notifications.comwrote:

    Ideally you would refactor your project into a multi module project so that you actually have a client, shared and server project each with its own build file. This would also separate the classpath so your GWT libraries do not interference with server libraries anymore.

    I guess that would be a good additional example.

    — Reply to this email directly or view it on GitHubhttps://github.com/steffenschaefer/gwt-gradle-plugin/issues/33#issuecomment-43098794 .

    / Hristo Stoyanov

    点赞 评论 复制链接分享
  • weixin_39722025 weixin_39722025 5月前

    About the first part (excluding server from GWT compilation): This isn't possible as the GWT compiler only gets the sources as part of the classpath. The classpath can only contain folders or jar files. The folders must contain valid java packages, so you can't simply reference a subpackage only.

    About the second part (excluding client stuff from Java compilation): Here you go: http://forums.gradle.org/gradle/topics/how_can_i_configure_certain_java_files_not_to_be_compiled

    From my personal view, I would never want the client code to be excluded from Java compilation for the following reasons: - Compiler checks + Lint tooling that needs class files - Unit tests testing client code need those class files - Dev Mode needs class files (I don't think it compiles the classes by itselves) -> ok this one will be obsolete with Super Dev Mode

    So let's come to alternative solutions ... You either have to introduce separate projects (You already said you don't want to do this) for each of the parts or put them in separate sourceSets:

    There are several facets of solving this. To make it as simple as possible, let's think about one thing: The only part that should be handled by both (GWT compiler and should be included in the war file) is the shared stuff. so let's put this into src/main/java.

    For the other two we need additional sourceSets:

    
    sourceSets {
        client {
            java {
                srcDir 'src/client/java' // I'm not sure if it's necessary to do this explicitly
           }
           compileClasspath = sourceSets.main.compileClasspath + sourceSets.main.output
       }
        server {
            java {
                srcDir 'src/server/java' // I'm not sure if it's necessary to do this explicitly
           }
           compileClasspath = sourceSets.main.compileClasspath + sourceSets.main.output
       }
    }
    

    Both sourceSets depend on the main sourceSet as both only compile with the shared stuff being available.

    To add the server classes to the war, you need:

    
    war {
        classpath sourceSets.server.output
    }
    

    For GWT you need to add the client sources:

    
    gwt {
        src += sourceSets.client.allJava.srcDirs
    }
    

    To make all GWT tasks to work as expected you should also need something like this:

    
    tasks.withType(de.richsource.gradle.plugins.gwt.AbstractGwtActionTask) {
       dependsOn tasks.compileClientJava
        classpath += sourceSets.server.output
    }
    

    I can't test this by now so this is all untested code. But I think it should lead to a working solution with proper separation and up to date checks but without separate projects.

    I hope this helps.

    点赞 评论 复制链接分享
  • weixin_39533432 weixin_39533432 5月前

    Thanks a lot, Steffen! I will give it a try ...but you see my pain: if i change a class I'm my server folder/package, there is no need to do get launch GWT compilation...

    /Hristo Stoyanov

    About the first part (excluding server from GWT compilation): This isn't possible as the GWT compiler only gets the sources as part of the classpath. The classpath can only contain folders or jar files. The folders must contain valid java packages, so you can't simply reference a subpackage only.

    About the second part (excluding client stuff from Java compilation): Here you go: http://forums.gradle.org/gradle/topics/how_can_i_configure_certain_java_files_not_to_be_compiled

    From my personal view, I would never want the client code to be excluded from Java compilation for the following reasons: - Compiler checks + Lint tooling that needs class files - Unit tests testing client code need those class files - Dev Mode needs class files (I don't think it compiles the classes by itselves) -> ok this one will be obsolete with Super Dev Mode

    So let's come to alternative solutions ... You either have to introduce separate projects (You already said you don't want to do this) for each of the parts or put them in separate sourceSets:

    There are several facets of solving this. To make it as simple as possible, let's think about one thing: The only part that should be handled by both (GWT compiler and should be included in the war file) is the shared stuff. so let's put this into src/main/java.

    For the other two we need additional sourceSets:

    sourceSets { client { java { srcDir 'src/client/java' // I'm not sure if it's necessary to do this explicitly } compileClasspath = sourceSets.main.compileClasspath + sourceSets.main.output } server { java { srcDir 'src/server/java' // I'm not sure if it's necessary to do this explicitly } compileClasspath = sourceSets.main.compileClasspath + sourceSets.main.output } }

    Both sourceSets depend on the main sourceSet as both only compile with the shared stuff being available.

    To add the server classes to the war, you need:

    war { classpath sourceSets.server.output }

    For GWT you need to add the client sources:

    gwt { src += sourceSets.client.allJava.srcDirs }

    To make all GWT tasks to work as expected you should also need something like this:

    tasks.withType(de.richsource.gradle.plugins.gwt.AbstractGwtActionTask) { dependsOn tasks.compileClientJava classpath += sourceSets.server.output }

    I can't test this by now so this is all untested code. But I think it should lead to a working solution with proper separation and up to date checks but without separate projects.

    I hope this helps.

    — Reply to this email directly or view it on GitHubhttps://github.com/steffenschaefer/gwt-gradle-plugin/issues/33#issuecomment-43117057 .

    点赞 评论 复制链接分享
  • weixin_39765840 weixin_39765840 5月前

    I have a project in which I tried to write such a "3-in-1" build file. I have defined the source sets a bit different, so maybe thats useful for you too:

    
    sourceSets {
      client.java.srcDirs = ['src'] // replaces standard src/main/java with src
      client.java.include 'com/example/app/client/**/*'
    
      // maybe use main.java instead to reuse the main source set.
      shared.java.srcDirs = ['src']
      shared.java.include 'com/example/app/shared/**/*'
    
      server.java.srcDirs = ['src']
      server.java.include 'com/example/app/server/**/*'
    }
    
    dependencies {
      // All dependencies for project import in IDEs
      compile configurations.clientCompile
      compile configurations.serverCompile
    
      // Dependencies defined per "sub module" for proper compilation
      clientCompile ... // dependencies for client/gwt compilation tasks
      serverCompile ... // dependencies for server related tasks
    }
    

    Later on you can merge client + shared (gwt compilation) and shared + server source sets (build server war) depending on the task. You also have separate dependencies which allows to configure a task with just the classpath it needs, e.g. gwt compilation does not need server dependencies.

    I haven't finished the build file because I switched to a multi module project but I think you get the idea. It is pretty similar to Steffen's approach.

    点赞 评论 复制链接分享

相关推荐