Sample Project, Gradle Setup


Objective

Import the sample REST_lamp, Gradle-based, project in Eclipse.

You can use the description that follows below to replace step #4 in the "Sample Project, Setup" document. When you're done you will have a fully functional REST project with Gradle as the build automation tool.

Download the sample project

  • Download the project file into your Downloads folder.
  • Uncompress the file and move the contents into your Eclipse workspace.
  •   
    $ cd ~/Downloads $ gunzip REST-Lamp-gradle.tar.gz $ tar xvf REST-Lamp-gradle.tar $ mv REST-Lamp-gradle <path_to_your_eclipse_workspace>
  • Install gradle (please note that sudo apt install gradle will install an older version of gradle that's not going to work for purposes of this tutorial):
  •   
    $ sudo mkdir /opt/gradle $ cd ~/Downloads $ wget https://services.gradle.org/distributions/gradle-6.3-bin.zip $ sudo unzip -d /opt/gradle gradle-6.3-bin.zip $ sudo echo "export GRADLE_HOME=/opt/gradle/gradle-6.3" >> /etc/profile.d/gradle.sh $ sudo echo "export PATH=\${GRADLE_HOME}/bin:\${PATH}" >> /etc/profile.d/gradle.sh $ source /etc/profile.d/gradle.sh $ gradle --version ------------------------------------------------------------ Gradle 6.3 ------------------------------------------------------------
  • Create the gradle wrapper and build the "executable":
  •   
    $ cd <path_to_your_eclipse_workspace> $ cd REST-Lamp-gradle $ gradle wrapper $ ./gradlew build

Import the sample project into your Eclipse workspace

  • Start Eclipse.
  • Go to "File > Import" and select "General > Existing Projects into Workspace"
  • Select the REST-Lamp-gradle project.

Final touches

You'll have to adjust slightly step #8 in the "Sample Project, Setup" document: instead of "REST_lamp" in this case we have "REST-Lamp-gradle".

That's all. Now you can select "Run > Run" and the project will be built and deployed to the local server. You can access the REST services at http://localhost:8080/REST-Lamp-gradle/lamps

Noteworthy

The two configuration files that are important are:

  • build.gradle under <path_to_your_eclipse_workspace>/REST-Lamp-gradle/
  • web.xml under <path_to_your_eclipse_workspace>/REST-Lamp-gradle/src/main/webapp/WEB-INF/ -- please note that this is the default gradle location for the file. Older tutorials have the WEB-INF directory under the project root, but that is no longer the case.

The deployable built is REST-Lamp-gradle.war under <path_to_your_eclipse_workspace>/REST-Lamp-gradle/build/libs/

Start a REST Gradle project from scratch

Here are the steps:

  • In Eclipse go to "New > Project" and select "Gradle project" then click "Next" and "Next" again.
  • Type in the name of the project and then "Finish". Wait until the project is created, then expand it in the left pane of Eclispe (the Package Explorer).
  • Expand the src folder; right click on main, select "New > Folder", and name it webapp.
  • Right click on webapp, select "New > Folder", and name it WEB-INF.
  • Right click on WEB-INF, select "New > Other" then "XML > XML File" and "Next"; name the file web.xml and then click "Finish".
  • Edit build.gradle and web.xml to suit the needs of your project. Just to get started you could use the same files we use in the sample REST-Lamp-gradle project. You can download the files from the links below:
  • Right click on the name of the project then select "Gradle > Refresh Gradle Project". NOTE: In Eclipse you need to do this every time you modify the buid.gradle file.

Last update: Apr 12, 2020 Virgil Bistriceanu cs445 Computer Science