DevOps Classroom notes 29/Nov/2024

Gradle

  • Gradle is a build tool for modern developement and CI/CD
  • Gradle was designed to work
    • with multiple languages
    • incremental builds
    • configure using groovy/kotlin language
  • Gradle introduced the concept for wrapper

To create a Java application using Gradle, you can follow this step-by-step tutorial that covers everything from project initialization to building and running your application. Below are the key steps and resources to guide you through the process.

Step 1: Install Gradle

Before you begin, ensure that you have Gradle installed on your machine. You can check if it’s installed by running the following command in your terminal:

gradle -v

If Gradle is not installed, you can download it from the Gradle website.

Step 2: Create a New Project

  1. Open your terminal and create a new directory for your project:

bash
mkdir my-java-app
cd my-java-app

  1. Initialize the Gradle project by running:

bash
gradle init --type java-application

This command sets up a basic Java application structure. You will be prompted to choose additional options such as the build script DSL (Groovy or Kotlin). For this tutorial, you can choose Groovy.

Step 3: Project Structure

After initialization, your project structure will look like this:

my-java-app/
├── build.gradle
├── settings.gradle
└── src
    └── main
        └── java
            └── mypackage
                └── App.java
  • build.gradle: The main build script for your project.
  • src/main/java: The directory where your Java source files will reside.

Step 4: Write Your Java Code

Open the App.java file located in src/main/java/mypackage and modify it to include a simple “Hello World” program:

package mypackage;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Step 5: Build the Application

To build your application, run the following command in your terminal:

gradle build

This command compiles your code, runs tests, and packages your application into a JAR file. You should see output indicating that the build was successful.

Step 6: Run Your Application

You can run your application using the Gradle run task. First, ensure that you have applied the application plugin in your build.gradle file:

plugins {
    id 'application'
}

mainClassName = 'mypackage.App'

Now run:

gradle run

This command will execute your Java application, and you should see “Hello, World!” printed in the terminal.

Step 7: Create an Executable JAR

To create an executable JAR file, ensure that your build.gradle includes the following configuration:

jar {
    manifest {
        attributes 'Main-Class': 'mypackage.App'
    }
}

After adding this, rebuild your project with gradle build. The JAR file will be located in the build/libs directory.

You can run the JAR file using:

java -jar build/libs/my-java-app.jar

Additional Resources

For more detailed guidance and examples, refer to these resources:
Building Java Applications Sample – Gradle User Manual [1].
Getting Started with Gradle | IntelliJ IDEA Documentation [2].
Gradle Tutorial for Complete Beginners [7].

These resources provide comprehensive insights into using Gradle for Java development and cover various advanced topics as well.

Citations:
[1] https://docs.gradle.org/current/samples/sample_building_java_applications.html
[2] https://www.jetbrains.com/help/idea/getting-started-with-gradle.html
[3] https://www.youtube.com/watch?v=-dtcEMLNmn0
[4] https://docs.gradle.org/current/userguide/part1_gradle_init.html
[5] https://gradle.org/guides/
[6] https://www.jetbrains.com/guide/java/tutorials/working-with-gradle/creating-a-gradle-project/
[7] https://tomgregory.com/gradle/gradle-tutorial-for-complete-beginners/
[8] https://github.com/spring-attic/gs-gradle
[9] https://stackoverflow.com/questions/50478309/using-gradle-to-build-a-very-simple-java-program
[10] https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-project-gradle.html


Building dotnet core applications

  • In dotnet applications we will have .csproj or .vbproj and you might have .sln files
  • dotnet projects will have configurations for build
    • Debug (Developers)
    • Release (For releases)
  • dotnet build command compiles the code and generates the files in bin folder
  • dotnet test command runs the unit tests
  • dotnet publish command copies all the necessary files into the folder passed
  • note: watch class room video for detailed build executions
  • Lets try running nopCommerce
  • install dotnet 9 SDK on ubuntu 24.04

Building reactjs applications

  • In react or node or angular js etc we donot have compilation phase
  • Generally we have to
    • resolve dependencies
    • test (run unit tests)
    • package
  • We need to install nodejs and npm, to make it easier to install and switch between multiple node js versions there is nvm (node version manager)

Installing nvm on ubuntu

  • nvm docs
  • Here’s a comprehensive list of commonly used npm (Node Package Manager) commands that every Node.js developer should know:

Basic Commands

  1. Initialize a New Project
  2. npm init: Initializes a new npm project and creates a package.json file.
  3. npm init -y: Initializes a new project with default settings.
  4. Install Packages
  5. npm install <package>: Installs a specific package and adds it to the dependencies in package.json.
  6. npm install: Installs all dependencies listed in package.json.
  7. npm install <package> --save-dev: Installs a package as a development dependency.
  8. Uninstall Packages
  9. npm uninstall <package>: Removes a specific package from the node modules and package.json.
  10. Update Packages
  11. npm update: Updates all packages to their latest versions based on the version range specified in package.json.
  12. npm update <package>: Updates a specific package to its latest version.

Listing Packages

  1. List Installed Packages
  2. npm ls: Displays the dependency tree for the current project, showing all installed packages.
  3. npm ls -g --depth=0: Lists globally installed packages without their dependencies.

Running Scripts

  1. Run Scripts Defined in package.json
  2. npm run <script-name>: Runs a script defined in the scripts section of your package.json.
  3. Common scripts include:
    • npm start: Starts the application.
    • npm test: Runs tests defined in the scripts.

Package Management

  1. Check for Outdated Packages
  2. npm outdated: Checks for outdated packages and lists them.
  3. Audit for Vulnerabilities
  4. npm audit: Scans your project for vulnerabilities in dependencies.
  5. npm audit fix: Automatically fixes vulnerabilities where possible.

Miscellaneous Commands

  1. Cache Management
  2. npm cache clean --force: Cleans the npm cache, which can help resolve installation issues.
  3. Publish Packages

    • npm publish: Publishes your package to the npm registry for others to use.
  4. View Package Information

    • npm view <package>: Displays information about a specific package from the npm registry.
  5. Search for Packages

    • npm search <keyword>: Searches the npm registry for packages matching the keyword.

Shortcuts

  • You can use shorthand commands:
  • Install: npm i <package>
  • Uninstall: npm un <package>
  • Update: npm up <package>

Conclusion

These commands are essential for managing Node.js applications effectively using npm. For further details, you can refer to the official npm documentation.

Citations:
[1] https://www.geeksforgeeks.org/15-npm-commands-that-every-node-js-developer-should-know/
[2] https://webreference.com/cheat-sheets/npm/
[3] https://docs.npmjs.com/cli/v6/commands/
[4] https://www.toptal.com/javascript/a-guide-to-npm-the-node-package-manager
[5] https://kinsta.com/knowledgebase/what-is-npm/
[6] http://dreamerslab.com/blog/en/npm-basic-commands/
[7] https://www.freecodecamp.org/news/npm-cheat-sheet-most-common-commands-and-nvm/
[8] https://www.milesweb.in/hosting-faqs/npm-commands/

Commands of attentition

  • npm install
  • npm run test
  • npm run build

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube