How to create Java gradle project

How to create Java Gradle project from command line?

It should create standard maven folder layout like on the picture below.

Java Gradle created with Eclipse plugin

UPDATE:

.1. From http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html I need to create file build.gradle with 2 lines

apply plugin: 'java'
apply plugin: 'eclipse'

.2. Add to build.gradle task below, than execute gradle create-dirs

task "create-dirs" << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}

.3. Then run gradle eclipse (or corresponding string to other IDE plugin configured)

So is there way to do it in one command?

128861 次浏览
Thanks.

great question - but unfortunately only a lame answer. I don't believe that it is currently possible to do what you propose because there are no initializers in UIStoryboard that allow overriding the view controller associated with the storyboard as defined in the object details in the storyboard on initialization. It's at initialization that all the UI elements in the stoaryboard are linked up to their properties in the view controller.

e it from your base class and add it as a sub view in the main view, typically self.view. Then you would simply use the storyboard layout with basically blank view controllers holding their place in the storyboard but with the correct view controller sub class assigned to them. Since they would inherit from the base, they would get that view.

It will by default initialize with the view controller that is specified in the storyboard definition.

  • create the layout in code and install it from your base view controller. Obviously this approach defeats the purpose of using the storyboard, but may be the way to go in your case. If you have other parts of the app that would benefit from the storyboard approach, it's ok to deviate here and there if appropriate. In this case, like above, you would just use bank view controllers with your subclass assigned and let the base view controller install the UI.

  • If you are trying to gain reuse of UI elements you created in the storyboard, they still must be linked or associated to properties in which ever view controller is using them for them to be able to "tell" the view controller about events.

    It would be nice if Apple came up with a way to do what you propose, but the issue of having the graphic elements pre-linked with the controller subclass would still be an issue.

    It's not that much of a big deal copying over a storyboard layout especially if you only need a similar design for 3 views, however if you do, you must make sure that all the previous associations are cleared, or it will get crashes when it tries to communicate to the previous view controller. You will be able to recognize them as KVO error messages in the log output.

    have a great New Year!!

    A couple of approaches you could take:

      be well

  • store the UI elements in a UIView - in a xib file and instantiate it from your base class and add it as a sub view in the main view, typically self.view. Then you would simply use the storyboard layout with basically blank view controllers holding their place in the storyboard but with the correct view controller sub class assigned to them. Since they would inherit from the base, they would get that view.

  • The gradle guys are doing their best to solve all (y)our problems ;-).

  • create the layout in code and install it from your base view controller. Obviously this approach defeats the purpose of using the storyboard, but may be the way to go in your case. If you have other parts of the app that would benefit from the storyboard approach, it's ok to deviate here and there if appropriate. In this case, like above, you would just use bank view controllers with your subclass assigned and let the base view controller install the UI.

  • They recently (since 1.9) added a new feature (incubating): the "build init" plugin.

    It would be nice if Apple came up with a way to do what you propose, but the issue of having the graphic elements pre-linked with the controller subclass would still be an issue.

    See: build init plugin documentation

    If you are using Eclipse, for an existing project (which has a build.gradle file) you can simply type gradle eclipse which will create all the Eclipse files and folders for this project.

    have a great New Year!!

    It takes care of all the dependencies for you and adds them to the project resource path in Eclipse as well.

    be well

    Finally after comparing all solution, I think starting from build.gradle file can be convenient.

    ild script. testCompile "junit:junit:4.11" }

    The result is like below.

    overview

    Gradle distribution has samples folder with a lot of examples, and there is gradle init --type basic comand see Chapter 47. Build Init Plugin. But they all needs some editing.

    You can use template below as well, then run gradle initSourceFolders eclipse

    /*
    * Nodeclipse/Enide build.gradle template for basic Java project
    *   https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/java/basic/build.gradle
    * Initially asked on
    *   http://stackoverflow.com/questions/14017364/how-to-create-java-gradle-project
    * Usage
    *   1. create folder (or general Eclipse project) and put this file inside
    *   2. run `gradle initSourceFolders eclipse` or `gradle initSourceFolders idea`
    * @author Paul Verest;
    * based on `gradle init --type basic`, that does not create source folders
    */
    
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    
    
    task initSourceFolders { // add << before { to prevent executing during configuration phase
    sourceSets*.java.srcDirs*.each { it.mkdirs() }
    sourceSets*.resources.srcDirs*.each { it.mkdirs() }
    }
    
    
    task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
    }
    
    
    // In this section you declare where to find the dependencies of your project
    repositories {
    // Use Maven Central for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    }
    
    
    // In this section you declare the dependencies for your production and test code
    dependencies {
    //compile fileTree(dir: 'libs', include: '*.jar')
    // The production code uses the SLF4J logging API at compile time
    //compile 'org.slf4j:slf4j-api:1.7.5'
    
    
    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile "junit:junit:4.11"
    }
    

    The result is like below.

    overview

    That can be used without any Gradle plugin for Eclipse,
    or with (Enide) Gradle for Eclipse, Jetty, Android alternative to Gradle Integration for Eclipse

    editbox

    To create a Java project: create a new project directory, jump into it and execute

    gradle init --type java-library
    

    Source folders and a Gradle build file (including a wrapper) will be build.

    I could handle it using a groovy method in build.gradle to create all source folders for java, resources and test. Then I set it to run before gradle eclipse task.

    eclipseClasspath.doFirst {
    initSourceFolders()
    }
    
    
    def initSourceFolders() {
    sourceSets*.java.srcDirs*.each { it.mkdirs() }
    sourceSets*.resources.srcDirs*.each { it.mkdirs() }
    }
    

    That can be used without any Gradle plugin for Eclipse,

    Now we can setup a new gradle Java EE project to eclipse with only one command. I put this example at GitHub

    Although it's not strictly a subclass, you can:

    1. option-drag the base class view controller in the Document Outline to make a copy
    2. Move the new view controller copy to a separate place on the storyboard
    3. That is not good enough.
    4. Change Class to the subclass view controller in the Identity Inspector
    I am switching to Intellij for gradle projects:

    enter image description here

    Here is what it worked for me.. I wanted to create a hello world java application with gradle with the following requirements.

      Here's an example from a Bloc tutorial I wrote, subclassing ViewController with WhiskeyViewController:

      animation of the above three steps

    1. The application has external jar dependencies
    2. This allows you to create subclasses of view controller subclasses in the storyboard. You can then use instantiateViewControllerWithIdentifier: to create specific subclasses.

    3. Create a runnable fat jar with all dependent classes copied to the jar
    4. Create a runnable jar with all dependent libraries copied to a directory "dependencies" and add the classpath in the manifest.

    Here is the solution :

    • Install the latest gradle ( check gradle --version . I used gradle 6.6.1)
    • Create a folder and open a terminal
    • Execute gradle init --type java-application
    • Add the required data in the command line
    • Import the project into an IDE (IntelliJ or Eclipse)
    • Edit the build.gradle file with the following tasks.

    Runnable fat Jar

    task fatJar(type: Jar) {
    clean
    println("Creating fat jar")
    manifest {
    attributes 'Main-Class': 'com.abc.gradle.hello.App'
    }
    archiveName "${runnableJar}"
    from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    } with jar
    println("Fat jar is created")
    }
    

    This approach is a bit inflexible: later modifications within the storyboard to the base class controller don't propagate to the subclass. If you have a lot of subclasses you may be better off with one of the other solutions, but this will do in a pinch.

    Copy Dependencies

    task copyDepends(type: Copy) {
    from configurations.default
    into "${dependsDir}"
    }
    

    Create jar with classpath dependecies in manifest

    task createJar(type: Jar) {
    println("Cleaning...")
    clean
    manifest {
    attributes('Main-Class': 'com.abc.gradle.hello.App',
    'Class-Path': configurations.default.collect { 'dependencies/' +
    it.getName() }.join(' ')
    )
    }
    from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    } with jar
    println "${outputJar} created"
    }
    

    Here is the complete build.gradle

    plugins {
    id 'java'
    id 'application'
    }
    repositories {
    mavenCentral()
    }
    dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.30'
    implementation 'ch.qos.logback:logback-classic:1.2.3'
    implementation 'ch.qos.logback:logback-core:1.2.3'
    testImplementation 'junit:junit:4.13'
    }
    def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
    def dependsDir = "${buildDir}/libs/dependencies/"
    def runnableJar = "${rootProject.name}_fat.jar";
    
    
    //Create runnable fat jar
    task fatJar(type: Jar) {
    clean
    println("Creating fat jar")
    manifest {
    attributes 'Main-Class': 'com.abc.gradle.hello.App'
    }
    archiveName "${runnableJar}"
    from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    } with jar
    println("Fat jar is created")
    }
    
    
    //Copy dependent libraries to directory.
    task copyDepends(type: Copy) {
    from configurations.default
    into "${dependsDir}"
    }
    
    
    //Create runnable jar with dependencies
    task createJar(type: Jar) {
    println("Cleaning...")
    clean
    manifest {
    attributes('Main-Class': 'com.abc.gradle.hello.App',
    'Class-Path': configurations.default.collect { 'dependencies/' +
    it.getName() }.join(' ')
    )
    }
    from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    } with jar
    println "${outputJar} created"
    }
    

    Gradle build commands
    Create fat jar : gradle fatJar
    Copy dependencies : gradle copyDepends
    mpile.collect { it.isDirectory() ? it : zipTree(it) } Create runnable jar with dependencies : gradle createJar

    More details can be read here : https://jafarmlp.medium.com/a-simple-java-project-with-gradle-2c323ae0e43d