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
- Open your terminal and create a new directory for your project:
bash
mkdir my-java-app
cd my-java-app
- 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
.csprojor.vbprojand you might have.slnfiles - 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
- Initialize a New Project
npm init: Initializes a new npm project and creates apackage.jsonfile.-
npm init -y: Initializes a new project with default settings. - Install Packages
npm install <package>: Installs a specific package and adds it to the dependencies inpackage.json.npm install: Installs all dependencies listed inpackage.json.-
npm install <package> --save-dev: Installs a package as a development dependency. - Uninstall Packages
-
npm uninstall <package>: Removes a specific package from the node modules andpackage.json. - Update Packages
npm update: Updates all packages to their latest versions based on the version range specified inpackage.json.npm update <package>: Updates a specific package to its latest version.
Listing Packages
- List Installed Packages
npm ls: Displays the dependency tree for the current project, showing all installed packages.npm ls -g --depth=0: Lists globally installed packages without their dependencies.
Running Scripts
- Run Scripts Defined in package.json
npm run <script-name>: Runs a script defined in the scripts section of yourpackage.json.- Common scripts include:
npm start: Starts the application.npm test: Runs tests defined in the scripts.
Package Management
- Check for Outdated Packages
-
npm outdated: Checks for outdated packages and lists them. - Audit for Vulnerabilities
npm audit: Scans your project for vulnerabilities in dependencies.npm audit fix: Automatically fixes vulnerabilities where possible.
Miscellaneous Commands
- Cache Management
-
npm cache clean --force: Cleans the npm cache, which can help resolve installation issues. -
Publish Packages
npm publish: Publishes your package to the npm registry for others to use.
-
View Package Information
npm view <package>: Displays information about a specific package from the npm registry.
-
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
