Proxy configuration for Gradle

6 05 2017

Are you using gradle behind corporate proxy? Here is how to configure proxy data for gradle (urls and credentials)

gradle.properties file

Gradle (and gradlew as well) is automatically reading gradle.properties file that is either in project directory or in USER_HOME/.gradle directory.

Inside of gradle.properties file set properties:

systemProp.http.proxyHost=http_proxy_ip_or_url
systemProp.http.proxyPort=port
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=pwd
systemProp.https.proxyHost=https_proxy_ip_or_url
systemProp.https.proxyPort=port
systemProp.https.proxyUser=username
systemProp.https.proxyPassword=password

The configuration above concerns both http and https traffic.

Inline properties

As an alternative you can specify properties in each gradle command by:

gradle build -Dhttp.proxyHost=http_proxy_ip_or_url ...




Run gradle project as java application

18 07 2015

gradle run

is not a default task in gradle. To use it, you need to enable the ‘application’ plugin. To enable the plugin add this line to your build.gradle:

apply plugin: 'application'

This plugin requires one variable to be configured – the main class name:

mainClassName = "com.looksok.Main"

Now you can execute the

gradle clean build run

to run your app





Compile gradle project with another project as a dependency

12 07 2014

imagesHaving project dependant on another project is common situation. How to configure gradle so that it will include your dependency project in build process?

There are two cases:

1. Your project is a root project and dependency is under its root

When dependand project is under root in a directory structure (dependency is not being shared with any other project except this one) you are doing it in recommended gradle strategy :)

To wrap up: you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
  |--Dependency
  |    |--build.gradle

Then to add Dependency to Project, you need to have Project/settings.gradle content like this:

include ':Dependency'

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

2. You have two independent projects and you need to use one of them as a dependency

When both project are on the same level in a directory structure you are not doing it as gradle team wish, but it is more real world situation in my opinion :) This is because you have your library project that can be easily used in several other projects as a dependency.

So you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
Dependency
  |--build.gradle

To add Dependency to the Project, you need to include it, and show Dependency path manually. So the Project/settings.gradle content should be like this:

include ':Dependency'
project(':Dependency').projectDir = new File(settingsDir, '../Dependency')

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

Notice that tis is build.gradle is exactly the same as in previous section.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Spring boot MVC application tutorial

21 06 2014

spring-logoI will tutor you to create simple Spring MVC application based on Spring boot. App will have two linked web pages with thymeleaf. The project itself will be made in Eclipse as an Gradle project. Let’s start!

Download Source Code

Source for this tutorial is available here, on GitHub

Create project with Gradle build file

Create build.gradle file. First of all depict repositories and dependencies that you will use for gradle plugins:

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.1.RELEASE")
    }
}

Then apply plugins for java, spring-boot and eclipse to have default targets of these plugins:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

Now show which java version compliance you want to have, and how your jar file will be named:

sourceCompatibility = 1.6

jar {
    baseName = 'looksok-mvc-demo'
    version =  '0.1.0'
}

And add repositories with dependencies used in your code:

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    testCompile("junit:junit")
}

Note: Use gradle version 1.1 or higher

Create web page templates in thymeleaf

Start with welcome page in src/main/resources/templates/hello.html with link to greeting page. If gradle did not automatically create directory structure, then create necessary folders on your own.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:th="http://www.thymeleaf.org" >
    <head>
        <title>Spring MVC Example</title>
    </head>
    <body>
        <h1>Welcome!</h1>

        <p>Click <a th:href="@{/greeting}">here</a> to advance to the next page.</p>
    </body>
</html>

and second template named greeting.html with link to previous page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:th="http://www.thymeleaf.org" >
    <head>
        <title>Spring MVC Example</title>
    </head>
    <body>
        <h1>Hello on a second page!</h1>

        <p>Click <a th:href="@{/hello}">here</a> to go back.</p>
    </body>
</html>

Configure MVC application components

Now with template pages you need to register controllers that will expose views (templates). Create java class with MVC Configuration (extending WebMvcConfigurerAdapter and market with @Configuration annotation). There override the addViewControllers method and add your views to the registry:

@Configuration
public class MvcConfig extends WebMvcAutoConfigurationAdapter {

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/hello").setViewName("hello");
		registry.addViewController("/greeting").setViewName("greeting");
	}
}

Make the Spring boot application executable
Spring boot will compile to Uber jar file with servlet engine embedded. To make it possible, the jar needs to have main class. This is how it should look like:

@EnableAutoConfiguration
@Configuration
@ComponentScan("pl.looksok.spring")
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Remember about annotations. The configuration annotation to indicate this is Spring config class, EnableAutoConfiguration of servlet engine and ComponentScan with package to scan (package is not mandatory when all @Configuration classes are in the same package).

Compile and run
Execute the command to build spring boot app:

gradle build compile

and to start the application:

java -jar [path_to_your_otput_jar]/looksok-mvc-demo-0.1.0.jar

To launch webpage, access the url:
localhost:8080\hello

Download Source Code

Source for this tutorial is available here, on GitHub

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: