Rename application.properties file in Spring Boot Application

1. Overview

By default, our Spring Boot application loads the external properties from the application properties file. This is normally available in our project resources folder. We can modify the name of application properties file, if required, even though it is a good convention to follow.

In this article, we’ll review various options to load the external properties file with a different name.

2. Setup

Once we create a Spring Boot project,

  • Let’s delete the application.properties file from the resources folder of our project.
  • Then, let’s create a customapp.properties file in the resources folder of our project. We’ll provide all our application configurations in this properties file.

spring.config.name property holds the name of our properties file in the Spring Boot application.

Therefore, we’ll set our properties file as the value for this property using various options.

3. Passing Property during Startup

First, let’s pass our properties file name as an argument to the spring.config.name property while starting the application:

java -jar SpringBootRenameAppProperties-0.0.1-SNAPSHOT.jar --spring.config.name=customapp

4. Environment Variable

In this section, let’s provide the new properties file name to the SPRING_CONFIG_NAME environment variable. Then, we can start the application from the command line:

set SPRING_CONFIG_NAME= customapp
java -jar SpringBootRenameAppProperties-0.0.1-SNAPSHOT.jar

5. System Property

Next, let’s set the configuration file name as the System Property before starting our Spring Boot application.

We’ll use spring.config.name as property key and our configuration file name as the value:

public static void main(String[] args) {
    System.setProperty("spring.config.name", "customapp");
    SpringApplication.run(SpringBootRenameAppPropertiesApplication.class, args);
}

6. Application Property

Finally, let’s pass our configuration file name as a value to spring.config.name property while creating our Spring Boot application:

public static void main(String[] args) {
    new SpringApplicationBuilder(SpringBootRenameAppPropertiesApplication.class)
	.properties("spring.config.name:customapp")
	.build()
	.run(args);
}

7. Conclusion

We reviewed various way to rename application properties file of our Spring Boot project. We can choose any one of these options to rename the file. Just note that, in all the cases we never added .properties to the file name. The framework appends it automatically to end of the file name. So if we pass customapp.properties as properties file name, then the application will be searching for customapp.properties.properties.

The code examples from this article are available on GitHub.

Advertisements

Change Tomcat Default Port

1. Overview

By default, Apache Tomcat runs on the port 8080. However, we can change the port if needed, mostly, when another application is already using this port.

In this short article, we’ll discuss how to change the Tomcat port.

2. Change Port

First, we need to navigate to the Conf folder inside the Tomcat root folder. Then, open the server.xml file.

In the server.xml file, we’ll find the port configuration similar to the below:

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /&gt;

Now, let’s change the connector port to 9090, which allows the Tomcat to run on 9090 instead of 8080:

    <Connector port="9090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /&gt;

Finally, restart the Tomcat to use the new port.

3. Conclusion

In this article, we discussed the process to change the port on which Tomcat is running.

Creating a New Spring Boot Project

1. Overview

In this article, we’ll discuss various ways to create a new Spring Boot project.

2. Using Spring STS

First, let’s create a Spring Boot project using STS(Spring Tool Suite).

STS is available for eclipse, Visual Studio and Atom IDE’s. We can download the latest version of STS from the Spring Tools site.

Let’s use eclipse flavor of STS in our article.

In STS, select File –> New –> Spring Starter Project:

Let’s provide our project information in the New Spring Starter Project screen:

The configurations available in this screen are :

  • Service URL — The spring boot project will be downloaded from this url. We don’t need to modify this since the Spring Boot templates are available here.
  • Name — name of our Spring Boot project
  • Type — indicates whether our project is a Maven or Gradle project
  • Packaging — configure if build will create a jar or a war
  • Java Version — the version of java to use for our project
  • Language — indicates which jvm language to use. The possible options are Java, Groovy and Kotlin
  • Group — the group id for our project
  • Artifact — artifact id of our project
  • Version — project version
  • Description — some information about our project
  • Package — the name of the root package

After configuring the properties, let’s click on Next to navigate to the New Spring Starter Project Dependencies screen:

The configurations available in this screen are:

  • Spring Boot Version — we’ll select the version of Spring Boot from the list of supported versions.

Next, let’s select the required dependencies from the list of available dependencies.

Finally, click on Next to complete providing the properties for our Spring Boot project.

We don’t need to provide any information in the next screen. Let’s just click on Finish to create our new Spring Boot project.

3. Using Spring Initializr

In this section, let’s create a new Spring Boot project using Initializr. This is particularly useful when STS installation is not available.

First, let’s navigate to the Spring Initializr site:

Similar to project creation on STS, this page has several configurable options:

  • Project — We can select either Maven or Gradle project
  • Language — In this option, we can select the programming language we’ll use in the project
  • Spring Boot — This section displays the list of supported Spring Boot versions. Let’s select our preferred Spring Boot version.
  • Group — we’ll provide Maven group id
  • Artifact — let’s provide artifact id in this field

Let’s click on More Options to provide more information on the project we are going to create:

  • Name — name of our Spring Boot project
  • Description — information about our project
  • Package Name — name of the root package of our project
  • Packaging — building Spring Boot projects can create either a jar or a war as a deployment artifact.
  • Java Version — select the java version for out Spring Boot project

Next, let’s select the dependencies needed for our project. After selecting the dependencies, let’s click on Generate Project:

This starts a zip file download. We’ll unzip the file and import the project into our IDE.

4. Starting Spring Boot Application

In the previous sections, we created a new Spring Boot project. In this section, let’s see how to run our new Spring Boot application in STS.

Right click on our project and select Run As –> Spring Boot App :

And, we’ll see messages similar to the below in the console indicating that our Spring Boot app is started:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-11 04:27:56.700  INFO 38676 --- [           main] c.p.s.SpringBootDemosApplication         : Starting SpringBootDemosApplication on ProgramSquare with PID 38676 (D:\programsquare\tutorials\SpringBootDemos\target\classes started by kiran in D:\programsquare\tutorials\SpringBootDemos)

5. Conclusion

In this article, we discussed various options to create a Spring Boot project. Then, we started the project from STS. We can use any one of the options to create a new Spring Boot project.

Disable Spring Boot Project Banner

1. Overview

When we start a Spring Boot application, it displays a similar banner message in the console:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

While it doesn’t cause any disruption, we can disable it, if required. In this article, we’ll review various options to disable the Spring Boot banner.

2. Using Properties

Similar to all other Spring Boot properties, we can disable the banner in our application properties:

spring.main.banner-mode=off

Now, let’s disable the banner using yml properties:

spring:
  main:
    banner-mode: "off"

3. Disabling In Program

Now, let’s see how to disable the banner programmatically. Let’s use setBannerMode method on SpringApplication class to turn off the banner:

public static void main(String[] args) {
    SpringApplication application = new SpringApplication(DisableBannerProgramApplication.class);
    application.setBannerMode(Mode.OFF);
    application.run(args);
}

4. Disabling during Startup

We can disable the banner without modifying the application properties or the code.

Let’s disable the banner passing the banner mode as a property:

java -Dspring.main.banner-mode=off -jar SpringBootDemo.jar

Now, let’s disable the banner by passing the banner mode as an argument to the Spring Boot application:

java -jar SpringBootDemo.jar --spring.main.banner-mode=OFF

5. Conclusion

In this article, we discussed various ways to turn off the Spring Boot banner. As always, the examples in this code are available on GitHub.

Change Server Port in Spring Boot

1. Overview

Typically, the Spring Boot apps run on the port 8080. However, in some cases we may need to run the apps on other ports. We can change the server port easily in Spring Boot.

2. Using Properties

Let’s change the server port using server.port property in our application properties:

server.port = 9000

In case, we are using yml, then we can change the port in application.yml properties file using server port property:

server:
  port: 9001

3. Using Command Line

We can also configure the port while launching the app. In this case, we don’t need to modify the application properties or the code.

Now, let’s provide the server port while launching the application using the cmd:

java -Dserver.port=9001 -jar SpringBootDemo.jar

Or, we can pass server port as an argument to the Spring boot application:

java -jar SpringBootDemo.jar –-server.port=9001

4. Changing Port Programmatically

Now, let’s change the server port programmatically by extending WebServerFactoryCustomizer interface:

@Component
public class ServerCustomization implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory &gt; {

	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9002);
	}
}

5. Conclusion

In this article, we discussed various options to change the server port for Spring Boot apps.

As always the code for all these examples is available on GitHub.

Installing Jenkins on Ubuntu

 

Java

 

Jenkins is created in Java and so requires JRE to be installed on the system to run. At the time of writing this, Jenkins supports Java 8. So check if Java is installed or not and install Java if it is not already installed on the system. You can follow these steps if you are not sure  about installing java on your Ubuntu machine.

 

Jenkins Installation

 

Run the below command to  download the package key from Jenkins repository and install it on the machine.

 

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
 
It should download and install Jenkins package key, but some times you may see failures similar to the below.

 

13

 

In case of these failures, run the same command in 2 different steps. Run the below 2 commands to download and install the jenkins key.

sudo wget –v –o https://pkg.jenkins.io/debian-stable/jenkins.io.key

 

sudo apt-key add jenkins.io.key

 

14

 

15

 

The distributions for Debian and Ubutunu are same, so don’t get confused with Debian term in the urls.

Once the jenkins key is successfully added to the machine, we will add jenkins distribution url to the ubuntu repository source list.

The sources list is configured in the /etc/apt/ folder, so navigate to that folder using the below command.

 

cd /etc/apt/

 

16

 

Run the below command to open the sources.list file and edit it.

sudo nano sources.list

18

 

Add the below line to the file and then save the file.

deb https://pkg.jenkins.io/debian-stable binary/

 

19

 

Since we added a new repository to the sources list, let us run an update to apply the changes we made.

 

sudo apt-get update

 

21

 

Once the package list is updated, let us start installing jenkins.

 

sudo apt-get install jenkins

22

As indicated in the above screen shot, it may fail due to missing dependency ‘Depends’.

 

The ‘Depends’ is available in universe repository, so install it before trying to reinstall Jenkins again.

 

sudo add-apt-repository universe

 

24

 

Once universe is installed successfully, try reinstalling jenkins again.

 

sudo apt-get install jenkins

 

25

 

Type ‘y’ when the installer asks whether you want to continue with the installation.

 

26

27

 

Jenkins is successfully installed on a Ubuntu machine.

 

Configuration

The jenkins configuration file is /etc/default/jenkins which contains lot of configurations related to jenkins instance.

Run the below command to review the jenkins configuration file.

cd /etc/default/

 

28

sudo nano jenkins

32

 

The configuration file contains information on the location of jenkins app, whether it is to run as a stand alone app or it is going to run in a web container, the location of jenkins war and port on which the app runs etc. We will review these configurations later.

29

30

31

 

The jenkins home directory should look similar to below. The jenkins home directory information is available in the above configuration file.

 

33

 

Start/Stop/Restart

 

To know the status of jenkins, run the below command.

sudo service jenkins status

 

34

If the jenkins is running without issues, then you can see the status as ‘active’ in the Active field in the above message. It also indicates when it was last started and how long it is running since last restart.

 

35

If the jenkins is stopped, then the Active field indicates ‘inactive’ along with the information on when it was shutdown.

 

You can run the below command to start jenkins.

 

sudo service jenkins start

Run the below command to stop jenkins.

sudo service jenkins stop

Run the below command to restart jenkins.

sudo service jenkins restart

36

 

Initial Jenkins Run

 

Once jenkins is installed successfully and you confirmed it is running successfully, open the jenkins url in browser to start configuring jenkins and using it.

 

Browse to <your host ip address>:8080 to open jenkins. Replace <your host ip address> with your ubuntu system ip.

 

You will see the below page when you access jenkins for the 1st time. It asks for one time Administrator password which is available at the below location on the machine jenkins is installed.

 

/var/lib/jenkins/secrets/initialAdminPassword

 

The path of the password file is indicated in the initial Jenkins page displayed when jenkins is accessed for 1st time.

 

37

 

Open the initialAdminPassword file and copy the password and paste it in the ‘Unlock Jenkins’ page and click on ‘Continue’.

image

 

The plugins used by Jenkins can be configured in the below page. Select ‘Install suggested plugins’ to stick with the default plugins suggested by Jenkins. You can manage the plugins at any point of time from Jenkins UI, so continue with the suggested plugins.

 

image

 

Jenkins will install the selected plugins.

 

image

 

After the plugins are installed, you can create the first admin user which can be used to manage Jenkins.

 

image

 

Provide username, password, full name, email id and click on ‘Save and Continue’ to create Jenkins admin user.

image

 

You can configure the jenkins url from the below screen. This can also be changed at any point of time, so leave it to the suggested value and click on ‘Save and Finish’.

image

 

Configuring Jenkins is now complete. Click on ‘Start using Jenkins’ to view the Jenkins home page.

 

image

 

The jenkins home page for the admin user created earlier is displayed.

image

 

Uninstall

 

Stop Jenkins service before uninstalling it. Then run the below command to uninstall jenkins

 

sudo apt-get remove --purge jenkins

Type ‘y’ when asked for confirmation.

 

image

 

Conclusion

 

In this post, we learnt how to install, run, configure and uninstall jenkins on Ubuntu. The later processes of creating and running jobs is same irrespective of where the jenkins instance is running.

Installing Java 8 on Ubuntu

 

First check if Java is already installed on the system. Run the below command to check if Java is already installed on the system.

 

java –version

 

If java is not installed on the system, you should see message similar to below.

1

 

If the java is already installed on the system, then it should display the version of java installed on the system.

 

11

 

If java is not installed on the system, then run the below command to start java installation.

sudo add-apt-repository ppa:webupd8team/java

Press [ENTER] to add the java repository information to the system.

2

3

 

Once java repository information is added, run the below command to update the package lists since java package is added to the list earlier.

 

sudo apt update

 

4

 

Run the below command to download and install java 8.

 

sudo apt install oracle-java8-installer

 

Enter ‘Y’ when the installer asks for confirmation to start java installation.

5

 

Select ‘Ok’, when the installer asks for accepting the java license.

 

6

 

Select ‘Yes’ when the  installer asks for accepting the Oracle license.

 

7

 

The download and installation of Java will take some time.

8

9

10

 

Run the below command to set the java version we have installed as the default version.

 

sudo apt install oracle-java8-set-default

 

12

Once the installation of java is complete, run the java version command again to check the java version.

 

java –version

 

11

 

 

Conclusion

 

java 8 (including JDK) is installed on Ubuntu and can be used for developing or running java applications.