0

Context: To create multiple executable jar's from a single project - multiple package gradle project

Issue: I refer to the solution provided in Link, this helps in generating the jar in build->libs folder, but when I try executing the jar nothing happens

Note: Even if I make the package name same as java file name, the generated jar does not execute.

Also I notice the file size of all the jar's generated is the same. Hope the issue faced is clear & await inputs as to where I am making a mistake.

My Project Structure (illustrative purpose):

ProjectA
-src
--main
---java
----pkg1
-----pkgCalculator         
------Calculator.java
-----pkgScale              
------Scale.java
----pkg2
-----pkgMusicPlayer        
------MusicPlayer.java
-----pkgVideoPlayer        
------VideoPlayer.java
---resources
----fxml
----css
--test
---java
---resources

Gradle file (relevant portion below, rest as per the link above ):

artifacts {
    archives jarPackage("pkgCalculator", "1.0"),
            jarPackage("pkgScale","1.0"),
            jarPackage("pkgMusicPlayer","1.0"),
            jarPackage("pkgVideoPlayer","1.0")

}
iCoder
  • 1,406
  • 6
  • 16
  • 35
  • you should check the content of the produced jar, and make sure that all needed compiled classes are included (in the correct package) and the MANIFEST is there with the right Main-Class attribute – M.Ricciuti Oct 16 '18 at 16:04
  • Tried with jar tvf command on the jar file, but it lists out way too many files & the cant scroll to the start in the terminal also guess it is due to the volume of entries displayed – iCoder Oct 16 '18 at 16:36
  • just to be sure: did you try to move class `pkg1.pkgCalculator.Calculator` to `calculator.Calculator` (remove first-level `pkg1`) ? and try to extract jar content in order to check the MANIFEST, there is probably wrong Main-Class attribute value there... – M.Ricciuti Oct 16 '18 at 16:46
  • Tried even that isn't working. Is there a way to pause the screen scrolling when we execute jar tvf , like page wise view or something? For if I execute just that screen scrolls too fast & cant see all things displayed. Checked - https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jar.html but couldnt find it – iCoder Oct 16 '18 at 17:04
  • you need to extract the jar in order to check MANIFEST content : `jar xf YOUR_JAR` – M.Ricciuti Oct 16 '18 at 17:10
  • the Manifest looks fine contains the version (manifest & implementation) also the title & main class. Nothing appears wrong in it atleast to me. – iCoder Oct 16 '18 at 17:34
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181962/discussion-between-m-ricciuti-and-icoder). – M.Ricciuti Oct 16 '18 at 17:35

1 Answers1

0

After discussion with @iCoder, first part of the issue has been solved: the jarPackage function could not be reused "as is", and had to be adapted to support generic class/package layout:

def jarPackage(String jarName, String className, artifactVersion) {
    if (artifactVersion == "" || artifactVersion == null) {
        artifactVersion = "1.0.0"
    }
    return tasks.create("jar${jarName}", Jar) {
        baseName = jarName

        version = artifactVersion

        def String pkgName = className.substring(0, className.lastIndexOf("."))
        def String pkgDir = pkgName.replaceAll("\\.", "/")
        def String clazzName = className.substring( className.lastIndexOf(".") +1 )
        from(sourceSets.main.output) {
            include "$pkgDir//**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$clazzName",
                    "Implementation-Version": "$version",
                    "Main-Class": "$pkgName.$clazzName"
        }
    }
}

artifacts {
    archives jarPackage("calculator", "pkg1.pkcalculator.Calculator" , "1.0.0")
}

Another issue remains, not related to Gradle but to JDK 11 linkage problem: @iCoder you should open another question for that remaining problem, if not fixed yet.

M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
  • once running the jarPackage function in the build, I'm getting the submodules that I need inside an extra jar in the build, but not getting the rest of it's dependenciess such as springboot dependencies.. is there a way to include the rest of my dependencies inside this extra jar? – USS-Montana Nov 16 '18 at 11:42