JavaFXPorts project as a library for other JavaFXPorts projects

239 views Asked by At

I currently struggle with probably very simple problem: How can I use one JavaFXPorts specific project as a dependency within another project of the same kind?

With pure separated projects, I have no idea how to combine them (have to admit, I'm not as up-to-date with Gradle, as I probably need to...).

I thought about using the apply plugin: 'maven' to install and grab the library to and from the local maven cache. But then there would be no separation of the platform specific code (everything - main, android, ios, desktop - would be merged together into one single JAR file.

Maybe if I reduce the problem to the pure part of the main source tree, I could create a standalone JavaFX-only project, this should be easy...

Or I could access the other project via a relative path. But for whatever reason I kind of dislike that idea.

What is the best practice for JavaFXPorts? I simply don't what to copy some stuff over and over into new projects (obviously).

Thanks in advance, Daniel

1

There are 1 answers

3
José Pereda On BEST ANSWER

This is a very simple example of how you can set a Gradle multi project, containing two subprojects:

  • Common: a regular JavaFX (gradle) project, with common code that can be reused later on in other projects.
  • GluonApplication: a simple Gluon Mobile project, that makes use of the common one.

I've used NetBeans to create the Gradle Root project and add the subproject, but it can be done from other IDEs or command line as well.

1. Gradle Root Project

Create a Gradle Root Project. Set the project name (GradleProject in this case), the location, and a Maven Group ID (com.gluonhq in this case), and a Maven Version (default 1.0-SNAPSHOT).

2. Gradle Common Subproject

Create a new Gradle Subproject. Choose a name (Common), and make sure that the location of this project is the GradleProject folder. Select a main class (com.gluonhq.common.Common).

Add some code:

package com.gluonhq.common;

public class Common {

    public static double sum(double a, double b) {
        return a + b;
    }
}

3. Gluon Mobile Subproject

Add a Gluon Mobile Subproject with the Gluon plugin for your IDE. For instance select Single View project. Choose a name (GluonApplication), and again make sure that the location of this project is the GradleProject folder. Select the package name (com.gluonhq.application) and the main class (GluonApplication).

You can run this project as is, from command line in the project root: gradle :GluonApplication:run, or from your IDE (tasks -> Run).

4. Include a Common dependency

Edit the build.gradle file from the Gluon Mobile subproject, and add the Common dependency.

Since both subprojects belong to the same root project you can simple do:

dependencies {
    compile 'com.gluonhq:charm:4.3.7'
    compile project(":Common")
} 

Save and reload the project. Now on your code you can call Common.sum:

button.setOnAction(e -> label.setText("3 + 4: " + Common.sum(3, 4)));

Run again and see that it works. You can deploy to mobile as well.

5. Installing the Common module

If you plan to reuse the Common project in this or other Gluon Mobile projects, you can install it in your .m2 repository.

On command line, from the project's root, run:

gradle :Common:install

and you'll see that the project is installed under <user>/.m2/repository/com/gluonhq/common/1.0-SNAPSHOT, including the common-1.0-SNAPSHOT.jar file.

6. Reusing the Common jar

Finally, you can include the common jar in any of your projects.

For that, edit the build.gradle file from the Gluon Mobile subproject, include the local repository, and add the Common dependency.

repositories {
    mavenLocal()
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

dependencies {
    compile 'com.gluonhq:charm:4.3.7'
    compile 'com.gluonhq:common:1.0-SNAPSHOT'
} 

Save and reload the project. And your common code will be available.

Of course, this works locally on your machine, but you can publish the artifact to a private or public repo as well, and then you should just include the proper repo in the above list.