I have a module called “import” which obviously contains some import data. It has to be a separate module.
Second module is just a Spring Boot application.
In module “import” I’ve got a simple task, which zips everything inside the /src/main/resources
task zipImport(type: Zip){
from "$project.projectDir/src/main/resources"
into "/"
archiveName "import.zip"
destinationDir project.buildDir
}
In server module, I would like to add this “import.zip” file to my resources - so that every time I build the project, server has the archive on the classpath. I know that I can create a copy task, and I can add “dependsOn” like:
task copyZip(dependsOn: ":import:zipImport", type: Copy) {
from "${project(":import").buildDir}/import.zip"
into "${project.buildDir}/resources/main"
}
processResources.dependsOn copyZip
but the IntelliJ does not invoke this task when I import the project. I would to have it automatically built. How can I add this to the sourceSets? Is this a good idea? Currently I’ve got something like:
sourceSets {
main {
resources {
task(":import:zipImport")
}
}
}
Basically, I would like to treat the src/main/resources as a source, and build ( which is import.zip ) as its product. I would like to attach it “as is” as a resource in spring.
The import project is just the source for the zip. From the business point of view it contains various images, documents etc which are then processed during the application startup. The import.zip file is also used in unit tests/integration tests which are run via IntelliJ & CMD to make sure that its contents fits to the current database scheme. Also, the archive form of this import is required - historical and integration reasons.
Currently, work with this piece looks like:
Ok guys, I want to add some data during import. I have to change the JPA entities inside the boot, unpack the old import.zip, change the required XLSXs, pack it back to zip and place it in spring-boot/src/main/resources and run test to make sure everything is ok.
So this is pretty much manual work. It would be perfect if the IDE ( in our case IntelliJ ) automatically updates ( or create a completely new ) archive - if something in import/src/main/resources changes before the IntelliJ’s run just like it does when some .java classes change.
I really don’t understand why it needs to be packed inside the spring boot jar. Why not have import.zip packed inside import-x.y.z.jar and put that on the classpath? Either way it can be obtained via ClassLoader.getResource("import.zip") in a test or at runtime in the application.
There is propably nothing that you’re not “getting”. The way how you described it is totally ok, it is way cleaner for me, but the solution you proposed simply didn’t work.
I tried to fix it somehow.
You proposed to use:
project(':spring-boot') {
evaluationDependsOn(':import') // not sure this is required but can't hurt
dependencies {
runtime project(path: ':import', configuration: 'importConfig')
}
But the boot.jar didn’t contain the import.zip file. To make it works I’ve change the runtime scope to compile and then ok - the file was in the jar’s /lib folder. But, I am not sure if it is SpringBoot-related, the classloader does not want to give me import.zip the way you described.
However, the @Chris_Dore solution works perfectly fine, probably because the zip file was placed in the jar’s /classes directory. It is not perfect, but it is acceptable.
Do you have any ideas how to improve your approach?
Assuming that :import project contains only resources and is not also a java project, here’s how I’d do it (note I’m using 'default' instead of default because it’s a keyword in java/groovy). I reckon that intellij might be happier with this approach too
project(':import') {
configurations.create('default')
task zip(type: Zip) {
from 'src/main/resources'
archiveName 'import.zip'
}
task jar(type: Jar) {
dependsOn zip
from zip.archivePath
}
artifacts.add('default', jar)
}
project(':spring-boot') {
dependencies {
compile project(':import') // references the 'default' configuration from the import project
}
}