|
| 1 | +# Extra Sources Example |
| 2 | + |
| 3 | +This example demonstrates the `extraSourceDirs` feature of the GWT Gradle Plugin, which allows you to include additional source directories in your GWT compilation. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `extraSourceDirs` property is useful in several scenarios: |
| 8 | + |
| 9 | +- **Multi-module projects**: Include sources from other Gradle modules |
| 10 | +- **Generated sources**: Include output from annotation processors or code generators |
| 11 | +- **Shared code**: Include common code that lives outside the main source tree |
| 12 | +- **Legacy code**: Include sources from non-standard directory structures |
| 13 | + |
| 14 | +## Project Structure |
| 15 | + |
| 16 | +``` |
| 17 | +extra-sources-example/ |
| 18 | +├── build.gradle # Main build configuration with extraSourceDirs |
| 19 | +├── extra-src/ # Additional source directory |
| 20 | +│ └── com/example/ |
| 21 | +│ ├── Extra.gwt.xml # GWT module for extra sources |
| 22 | +│ └── extra/ |
| 23 | +│ └── ExtraSourceClass.java |
| 24 | +└── src/main/java/ |
| 25 | + └── com/example/ |
| 26 | + ├── MyModule.gwt.xml # Main GWT module |
| 27 | + ├── client/ |
| 28 | + │ └── MyEntryPoint.java |
| 29 | + └── shared/ |
| 30 | + └── SharedClass.java |
| 31 | +``` |
| 32 | + |
| 33 | +## Configuration |
| 34 | + |
| 35 | +The key configuration in `build.gradle`: |
| 36 | + |
| 37 | +```gradle |
| 38 | +gwt { |
| 39 | + gwtVersion = '2.12.1' |
| 40 | + modules = ['com.example.MyModule'] |
| 41 | + |
| 42 | + // Example of adding extra source directories |
| 43 | + // These will be automatically added to both GWT compilation and Java source sets |
| 44 | + // This could be sources from another module or generated sources |
| 45 | + extraSourceDirs = files("$projectDir/extra-src") |
| 46 | + |
| 47 | + compiler { |
| 48 | + style = 'PRETTY' |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Key Features Demonstrated |
| 54 | + |
| 55 | +### 1. Automatic Source Set Integration |
| 56 | +When you specify `extraSourceDirs`, the plugin automatically: |
| 57 | +- Adds the directories to the Java source sets |
| 58 | +- Includes them in the GWT compilation classpath |
| 59 | +- Ensures proper dependency tracking for incremental builds |
| 60 | + |
| 61 | +### 2. Cross-Directory Module References |
| 62 | +The example shows how code in the main source directory can reference and use classes from the extra source directory: |
| 63 | + |
| 64 | +- `ExtraSourceClass` in `extra-src/` is available to the main module |
| 65 | +- The GWT module can inherit from modules defined in extra source directories |
| 66 | +- All sources are compiled together seamlessly |
| 67 | + |
| 68 | +### 3. Multiple Source Directories |
| 69 | +You can specify multiple extra source directories: |
| 70 | + |
| 71 | +```gradle |
| 72 | +extraSourceDirs = files( |
| 73 | + "$projectDir/extra-src", |
| 74 | + "$projectDir/generated-src", |
| 75 | + "../shared-module/src/main/java" |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +## Usage Scenarios |
| 80 | + |
| 81 | +### Multi-Module Projects |
| 82 | +```gradle |
| 83 | +// In a multi-module setup |
| 84 | +gwt { |
| 85 | + modules = ['com.example.MyApp'] |
| 86 | + extraSourceDirs = files( |
| 87 | + project(':shared-core').file('src/main/java'), |
| 88 | + project(':shared-ui').file('src/main/java') |
| 89 | + ) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Generated Sources |
| 94 | +```gradle |
| 95 | +// Include annotation processor outputs |
| 96 | +gwt { |
| 97 | + modules = ['com.example.MyApp'] |
| 98 | + extraSourceDirs = files('build/generated/sources/annotationProcessor/java/main') |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Custom Source Generation |
| 103 | +```gradle |
| 104 | +task generateSources(type: Exec) { |
| 105 | + // Your source generation logic |
| 106 | + outputs.dir 'src/generated/java' |
| 107 | +} |
| 108 | +
|
| 109 | +gwt { |
| 110 | + modules = ['com.example.MyApp'] |
| 111 | + extraSourceDirs = files('src/generated/java') |
| 112 | +} |
| 113 | +
|
| 114 | +// Ensure sources are generated before GWT compilation |
| 115 | +tasks.gwtCompile.dependsOn(generateSources) |
| 116 | +``` |
| 117 | + |
| 118 | +## Running the Example |
| 119 | + |
| 120 | +1. **Compile the project:** |
| 121 | + ```bash |
| 122 | + ./gradlew gwtCompile |
| 123 | + ``` |
| 124 | + |
| 125 | +2. **Run in development mode:** |
| 126 | + ```bash |
| 127 | + ./gradlew gwtDevMode |
| 128 | + ``` |
| 129 | + |
| 130 | +3. **Build the complete project:** |
| 131 | + ```bash |
| 132 | + ./gradlew build |
| 133 | + ``` |
| 134 | + |
| 135 | +## Expected Output |
| 136 | + |
| 137 | +When you run `gwtCompile`, you should see that: |
| 138 | +- Sources from both `src/main/java` and `extra-src` are included in compilation |
| 139 | +- The `ExtraSourceClass` is available to the main application |
| 140 | +- The GWT compiler successfully processes all sources together |
| 141 | + |
| 142 | +## Advanced Configuration |
| 143 | + |
| 144 | +### Per-Mode Configuration |
| 145 | +You can also configure different extra sources for different modes: |
| 146 | + |
| 147 | +```gradle |
| 148 | +gwt { |
| 149 | + modules = ['com.example.MyApp'] |
| 150 | + extraSourceDirs = files('common-extra-src') |
| 151 | + |
| 152 | + compiler { |
| 153 | + extraSourceDirs = files('common-extra-src', 'compile-only-src') |
| 154 | + } |
| 155 | + |
| 156 | + devMode { |
| 157 | + extraSourceDirs = files('common-extra-src', 'dev-only-src') |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### With FileCollections |
| 163 | +You can use Gradle's `FileCollection` for more complex scenarios: |
| 164 | + |
| 165 | +```gradle |
| 166 | +configurations { |
| 167 | + extraSources |
| 168 | +} |
| 169 | +
|
| 170 | +dependencies { |
| 171 | + extraSources files('path1', 'path2') |
| 172 | + extraSources project(':other-module').sourceSets.main.java.srcDirs |
| 173 | +} |
| 174 | +
|
| 175 | +gwt { |
| 176 | + modules = ['com.example.MyApp'] |
| 177 | + extraSourceDirs = configurations.extraSources |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +This example demonstrates the flexibility and power of the `extraSourceDirs` feature for handling complex project structures and source management scenarios. |
0 commit comments