Start with me from scratch. Jblog Project (ii) from MAVEN to Gradle

Source: Internet
Author: User
Tags maven central

From scratch, do it with me. Jblog Project (a) Introduction

Starting from scratch, do it with Me Jblog Project (ii) Maven

Start with me from scratch. Jblog Project (ii) from MAVEN to Gradle

The previous section described the Project Builder tool maven, which is not very powerful in managing jar packages

When bloggers introduced the spring framework using MAVEN, they learned that spring recommended using the Gradle tool to build the project

Baidu a bit, found Gradle is also a set of excellent project building tools

Gradle uses the groovy language to configure projects and dependencies, greatly increasing flexibility and configuration, and even configuring code much less than the XML structure of POM files

In view of my desire to make jblog a modern small personal blogging system

And Gradle on the home page to introduce themselves as modern Open-source Enterprise Build Automation

So I decided to shift the project prototype from MAVEN build to Grable build

The current tutorial on Gradle is relatively fragmented, and below I'll step-by-step to demonstrate how to integrate Gradle into MyEclipse

First, download the latest version of Gradle on the Gradle website.

It's http://gradle.org/gradle-download/.

Bo Master downloaded the 2.8 version, wrote this article has been updated to 2.9

Download and unzip to any directory

Then configure the Windows environment variables, a total of three to configure, as follows:

Gradle_home: Your grable decompression path, for example: D:\Workspaces\gradle-2.8

Gradle_user_home: You want to build a path to your local repository, named. Grable, for example: D:\Workspaces\.gradle

Path: This needless to say, the system path, can be directly knocked in the cmd grable command, the path is added at the end:%gradle_home%\bin;

These three configurations, we initially verify, open cmd, any path under the Gradle-v command, the following response, even if the installation was successful

For detailed command and usage of Gradle, you can refer to the following tutorial

http://wiki.jikexueyuan.com/project/gradle/

It is recommended to quickly read the tutorial above and we will implant the grable into the Eclipse/myeclipse

Open eclipse, go to marketplaces, search for Grable, find Gradle IDE Pack, pass the blogger test this plugin is the best use

After you install the Gradle IDE Pack plugin, restart Eclipse, create a new simplest Java Project, or try the Web Projec

Create a new Build.gradle file under the project root directory,

Edit the Build.gradle configuration file with the following example:

Apply plugin:"Java"Apply plugin:"Eclipse"sourcecompatibility= 1.8version='1.0'configurations {provided}sourcesets {Main.compileclasspath+=configurations.provided Test.compileclasspath+=configurations.provided Test.runtimeclasspath+=configurations.provided}repositories {mavenlocal () Maven {URL"http://maven.oschina.net/content/groups/public/"} mavencentral ()}dependencies {compile"org.hibernate:hibernate-core:5.+"Testcompile"junit:junit:4.7"provided"javax.servlet:javax.servlet-api:4.+"}//Note that the following configuration, the new version gradle will cause eclipse frequent errors if you do not use the array additions eclipse.classpath.plusConfigurations+ = [configurations.provided]

After saving the Build.gradle file, right-click Project, select Configure, select Convert to Gradle project, add the Gradle attribute to the project

After adding the Gradle attribute, let's have eclipse add several dependencies defined in Build.gradle to the project

Before that, you need to configure the Gradle IDE Pack plugin, Eclipse's window->preferencecs->gradle, to configure our local Gradle path

After the configuration is ready, we have to experiment with how to bring the dependencies defined in the Build.gradle file into the eclipse environment

Right-click Project Project, select Gradle, select Enable Dependency Management, let plug-in to open dependency management

After opening, still right-click the project, Gradle->refresh Dependencies, refresh the dependency list

In the background, Gradle will retrieve the MAVEN local repository in our system, the MAVEN central warehouse, and the Chinese mirror warehouse provided by Chinaos, as defined in the above file.

Download the required packages to the configured Gradle local repository and introduce them to Eclipse's classpath, as shown in the following:

At this point, all the work on Gradle integration into eclipse is over, and if you want to continue with the introduction of the jar package in the project authoring process, add the Build.gradle file

Then refresh the Gradle dependencies and add them to eclipse's environment in real time.

Finally, focus on the scope of provided (effective dependency range), the previous article on Maven, where the configuration depends on the scope commonly used in the following types of

Compile: Compile, test, and package with dependencies such as spring, hibernate, struts, etc.

Test: Need to rely on testing, common with JUnit

Provided: usually provided by the container, only need to write code, such as Servlet

Runtime: Packaging runtime requires dependency

After moving to Gradle, the Gradle default only provides us with four scopes, as follows:

Compile: Compilation scope dependencies are available in all classpath, and they are also packaged

Runtime: Depends on the time required to run and test the system, but it is not required at compile time. For example, you might only need the JDBC API JAR when compiling, but only if you need the JDBC driver to run it.

Testcompile: Additional dependencies required for test-time compilation

Testruntime: Test Run time required

You can see that Gradle does not provide a provided-like scope by default, such as when we need to write Web project, we need to introduce a servlet package, which is more troublesome

We can manually add the servlet package under Tomcat to Eclipse's build path, but that's too low, after all we've chosen to use the project Builder to manage our jar packages.

Don't worry, Gradle. After all, a programming language is used to configure the project build, so its flexibility is quite high, in the above configuration file, we have defined a provided scope

And this provided uses the Sourceset collection to define the scope, to clarify the need for dependencies during the compile of the main directory, and to rely on the test directory during compilation and run time

The implication is that there is no need to rely on the main directory during the run, that is, the runtime does not need to package this provided scope dependency

Finally, with the Eclipse plug-in, this provided scope is added to Eclipse's classpath, and Eclipse introduces the servlet to project dependencies.

Start with me from scratch. Jblog Project (ii) from MAVEN to Gradle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.