Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Ubuntu 16.04 Maven dependencies download

  1. #11
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ubuntu 16.04 Maven dependencies download

    r-senior you are a hero.

    The information you gave was super helpful and resulted in more information that i was seeking, which was impossible for Maven for me.
    So far super awesome.

    I followed the guides you have linked, which resulted in a working gradle installation.
    I have installed the gradle plugin shipbuilder in Eclipse and of i am.

    I tried to follow the gradle guide to create a new web application, but ended up with an application where i could not reach index.html,
    and i was greeted with a "Directory: project/path" message from the gradle web service.

    I than tried to see if i could create a a gradle ready dynamic web project, but it does not seem that their is any "easy" way to do this?
    So you get the file and folder structure from the project template along with the tomcat library and web module version.
    Last edited by Drenriza; March 9th, 2018 at 12:23 PM.

  2. #12
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Ubuntu 16.04 Maven dependencies download

    Quote Originally Posted by Drenriza View Post
    I tried to follow the gradle guide to create a new web application, but ended up with an application where i could not reach index.html,
    and i was greeted with a "Directory: project/path" message from the gradle web service.
    I don't know why that would be. What version of gradle are you running and when do you get this message?

    Could you post the output of "ls -R" from the root of your project so that I can see the file structure you created?

    I than tried to see if i could create a a gradle ready dynamic web project, but it does not seem that their is any "easy" way to do this?
    So you get the file and folder structure from the project template along with the tomcat library and web module version.
    Do you mean an Eclipse "Dynamic Web Project"? This will have the wrong directory structure for Gradle/Maven.

    Something that Maven does do better than Gradle is "archetypes", which are template projects. Gradle doesn't really have this concept so creating projects is more of a manual process. But it's not difficult and you end up deleting things you don't want from an archetype anyway:

    Code:
    $ mkdir MyProject
    $ cd MyProject
    $ mkdir -p src/{main,test}/java/my/package
    $ mkdir -p src/main/webapp
    ... where "my/package" is replaced by whatever package structure you want, e.g. org/ubuntu/demo.

    Obviously you'd add a minimal configuration to build.gradle, e.g.

    Code:
    plugins {
        id 'war'  
    }
    
    repositories {
        jcenter()
    }
    
    dependencies {
        providedCompile 'javax.servlet:javax.servlet-api:3.1.0' 
        testCompile 'junit:junit:4.12'
    }
    You can then either use the Buildship plugin to make an Eclipse project (File - Import - Gradle - Existing Gradle Project), or you can use Gradle to do it:

    https://docs.gradle.org/current/user...se_plugin.html

    You might try this in your build.gradle (note the 'eclipse-wtp' plugin)

    Code:
    plugins {
        id 'war'  
        id ''eclipse-wtp'
    }
    
    ...
    Then ask Gradle to make an Eclipse project:

    Code:
    $ gradle eclipse
    Personally I don't use the IDE plugins. I prefer importing a basic Gradle project into the IDE but you might want to try.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #13
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ubuntu 16.04 Maven dependencies download

    Thanks for your reply 2r-senior

    I took a fresh crack at Gradle again and managed to get a working website, meaning that the projects is now built / compiled
    and with Tomcat i can see the webpage as expected (2 pages).

    I ended up with a build.gradle as seen below
    Code:
    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'eclipse-twp'
    apply plugin: 'com.bmuschko.tomcat'
    
    repositories {
        jcenter()
    }
    
    dependencies {
        providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
        testCompile 'junit:junit:4.12'
    }
    
    /*
    TOMCAT BUILD CONFIGURATION
    */
    repositiroes {
        mavenCentral()
    }
    
    dependencies {
        def tomcatVersion = '8.5.29'
        tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
        "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
    }
    
    tomcat {
        httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
        ajpProtocol = 'org.apache.coyote.ajp.AjpNio2Protocol'
    }
    
    /*
    TOMCAT BUILD SCRIPT BUILD CONFIGURATION - EXTERNAL DEPENDENCIES
    */
    buildscript {
        repositories {
            jcenter()
        }
        
        dependencies {
            classpath 'com.bmuschko:gradle-tomcat-plugin:2.4.2'
        }
    }
    I am not entirely sure what all this does, for example it seems that all the Tomcat lines is for having a working local Tomcat server, that you can use for tests on your local machine but i was not able to find
    any folders or files that would indicate this in the project folder. Nor can i see that gradle has downloaded a local copy of Tomcat 8.5.29 in /opt/gradle.

    It also seems that Gradle understand my structure (woohoo), since it adheres to the web.xml that i used to set the welcome page.
    - src
    - main
    - java
    - resources
    - webapp
    - META-INF
    - WEB-INF
    * web.xml
    - lib
    I am thinking this is due to the war plugin? I have not found a site / quote that actually confirms this.

    All in all i now have a working Gradle project, win. So lets see if i now can also get a working oAuth2 project working.

    @r-senior I have a question at this point.
    When i create a normal "dynamic web application" through Eclipse, it also includes a 'deployment descriptor' that in Eclipse
    gives an easy overview of how your web.xml is structured and linked. Do you know if i can add this in Eclipse to my Gradle project? Since it's nice for the eyes and ones understanding

    I know that web.xml is considered an older 'technology' but i prefer it over annotations.
    Last edited by Drenriza; March 13th, 2018 at 02:12 PM.

  4. #14
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Thumbs up Re: Ubuntu 16.04 Maven dependencies download

    Quote Originally Posted by Drenriza View Post
    I took a fresh crack at Gradle again and managed to get a working website, meaning that the projects is now built / compiled
    and with Tomcat i can see the webpage as expected (2 pages).
    Good progress!

    Some suggestions for your build.gradle ...

    1. Be careful with typing. On line 3 you have "eclipse-twp" but this should be "eclipse-wtp". On line 18, you have "repositiroes" but this should be "repositories". These two lines will be being ignored.

    2. Yes, the "war" plugin is the plugin that understands the "webapp" part of the project:

    https://docs.gradle.org/current/user...ar_plugin.html

    Note that the "war" plugin extends the "java" plugin, so you don't need to include that one.

    3. You will see many examples with "apply plugin" at the top but the newer form is like this (which is neater and less to type):

    Code:
    plugins {
        id 'war'
        id 'eclipse-wtp'
        id 'com.bmuschko.tomcat' version '2.4.2'
    }
    Note that you don't need the buildscript section any more:

    https://plugins.gradle.org/plugin/com.bmuschko.tomcat

    So I think your build.gradle can be simplified to:

    Code:
    plugins {
        id 'war'
        id 'eclipse-wtp'
        id "com.bmuschko.tomcat" version "2.4.2"
    }
    
    repositories {
        jcenter()
    }
    
    dependencies {
        def tomcatVersion = '8.5.29'
        tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
        "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
    
        providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
        testCompile 'junit:junit:4.12'
    }
    
    tomcat {
        httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
        ajpProtocol = 'org.apache.coyote.ajp.AjpNio2Protocol'
    }
    I am not entirely sure what all this does ...
    Neither am I . The simplified version has four sections:

    1. The Gradle plugins that you want to use.
    2. The repositories where Gradle should search for dependencies. Usually mavenCentral() or jcenter().
    3. A list of dependencies that Gradle will add to your classpath. It will add their dependencies (transitive dependencies) too.
    4. Specific configuration for your Tomcat plugin.

    , for example it seems that all the Tomcat lines is for having a working local Tomcat server, that you can use for tests on your local machine but i was not able to find any folders or files that would indicate this in the project folder.
    You have added a Tomcat plugin that provides a Tomcat server that Gradle can control. This plugin adds a "tomcatRun" task so that you can run your web application on Tomcat like this:

    Code:
    ./gradlew tomcatRun
    Note that you don't actually need this. You could just make a WAR file and copy it to the auto-deploy directory of an existing Tomcat installation if you have one. This Tomcat plugin is just a convenient way to start an embedded Tomcat server from Gradle.

    See how you get on with this Tomcat plugin. I think you might find the Gretty plugin more useful but we can leave that for another day. It should be simple to switch from one server plugin to another.

    Nor can i see that gradle has downloaded a local copy of Tomcat 8.5.29 in /opt/gradle
    Gradle downloads dependencies to a user cache directory, which is usually ~/.gradle/caches. It will be under there ... somewhere

    When i create a normal "dynamic web application" through Eclipse, it also includes a 'deployment descriptor' that in Eclipse
    gives an easy overview of how your web.xml is structured and linked.
    The web.xml is the deployment descriptor but I think I know what you mean:

    eclipse-web-xml.png

    I think if you type "eclipse-wtp" correctly in your build.gradle, then run the "eclipse" task to make an Eclipse project, it will work.

    Code:
    ./gradlew eclipse
    In Eclipse, "File - Open Projects from File System" to open the project directly.

    I've put my version of this demo project on Github in case I want to try things out as you make progress with it:

    https://github.com/sanhozay/WebDemo
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  5. #15
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ubuntu 16.04 Maven dependencies download

    Thanks for your reply @r-senior!

    Ye i had a few typos in my gradle.build, thanks.
    I knew these were their because it worked, but when i wrote my reply here i was forced to write it by hand and introduced them (my bad).
    This is cause the machine i tested Gradle on is my anti-virus VM machine that cannot copy to the clipboard of the host.

    I tried the
    Code:
    plugin {
    id 'plugin'
    }
    but my Gradle would not accept this structure, which is why i used the other one as you see.
    Also thanks for the simplification example of the build file, gives alot in terms of understanding.

    Thanks! i found the embedded tomcat under ~/.gradle/caches just as you said.
    This is nice to know.

    Quick question @r-senior
    We added the javax-servlet api with
    Code:
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    But how did you know to call it javax.servlet:javax.servlet-api:3.1.0 and not something else?
    Also how can i through providedCompile add the javax.servlet.http.HttpServlet-api .jar file?

    I looked here https://mvnrepository.com/artifact/j...-api/2.2.1-b03 but i have been told that gradle do not support the command
    Code:
    // https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
    provided group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2.1-b03'
    Last edited by Drenriza; March 17th, 2018 at 03:22 PM.

  6. #16
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ubuntu 16.04 Maven dependencies download

    Sneeky sneeky

    But how did you know to call it javax.servlet:javax.servlet-api:3.1.0 and not something else?
    Also how can i through providedCompile add the javax.servlet.http.HttpServlet-api .jar file?
    So at https://mvnrepository.com/artifact/j...ax.servlet-api it says
    Code:
    // https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
    provided group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2.1-b03'
    But gradle (as far as i am told) does not have a provided group method. So one can use providedCompile which makes the
    jar file available to the project at runtime, but is excluded from the .war container.

    So the command is as follows
    Code:
    // https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
    provided group: 'javax.servlet.jsp'(1), name: 'jsp-api(2)', version: '2.2.1-b03(3)'
    Code:
    providedCompile 'javax.servlet.jsp(1):jsp-api(2):2.2.1-b03(3)'
    This is what i see works through trial and error.

  7. #17
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Ubuntu 16.04 Maven dependencies download

    When you write a Gradle build file, you are actually writing Groovy code. We don't need to get too deep into Groovy and the way it implements Domain Specific Languages (DSLs), but when you declare a dependency, you are actually calling a method:

    Code:
    providedCompile([group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'])
    The argument there is a Map (just like a Java Map) with three keys "group, name and version". You can declare your dependency like this and it will work.

    Groovy has a few shortcuts that you can use to make the syntax more readable. Let's look at these step by step:

    1. Leave out the square brackets for the map. This is how Groovy provides named parameters:

    Code:
    providedCompile(group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0')
    2. Leave out the parentheses for the function call

    Code:
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    3. Gradle then allows you to just pass a string, where the group, name and version are delimited by ':'. I couldn't find the relevant source code but I assume this string is broken up using a string tokenizer and converted to a map.

    Code:
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    When you look for dependencies on mvnrepository.com, it will give you the form in (2) and make some assumptions about how you would declare the dependency, i.e. "provided".

    Code:
    provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    Provided means the JARs are provided by something outside your project, e.g. Tomcat. So they won't be packaged into your WAR file. Because some APIs, e.g. the servlet API have compile-time dependencies (the Java interfaces) and runtime dependencies (the implementations of those interfaces), the Gradle WAR plugin distinguishes them using "providedCompile" and "providedRuntime". The "providedCompile" dependencies are needed to compile your project. The "providedRuntime" are needed when Gradle runs your project.

    It is confusing, I know. The suggestion on mvnrepository.com is not how it works with the Gradle WAR plugin. You need "providedCompile".

    In your case, you want the servlet API to be used during compilation, so you can use either:

    Code:
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    or

    Code:
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    You don't need a "providedRuntime" for the servlet API because that is handled by your Tomcat plugin.

    You figured out what you needed to do. The above explanation is just there to fill in a few gaps and you don't need to understand it to use Gradle.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •