A basic Java project
Let’s look at a simple example. To use the Java plugin, add the following to your build file:
Example: Using the Java plugin
build.gradle
apply plugin: 'java'
Note: The code for this example can be found at
samples/java/quickstart
in the ‘-all’ distribution of Gradle.
This is all you need to define a Java project. This will apply the Java plugin to your project, which adds a number of tasks to your project.
What tasks are available?
You can use
gradle tasks
to list the tasks of a project. This will let you see the tasks that the Java plugin has added to your project.
Gradle expects to find your production source code under
src/main/java
and your test source code under src/test/java
. In addition, any files under src/main/resources
will be included in the JAR file as resources, and any files under src/test/resources
will be included in the classpath used to run the tests. All output files are created under the build
directory, with the JAR file ending up in the build/libs
directory.
The Java plugin adds quite a few tasks to your project. However, there are only a handful of tasks that you will need to use to build the project. The most commonly used task is the
build
task, which does a full build of the project. When you run gradle build
, Gradle will compile and test your code, and create a JAR file containing your main classes and resources:
Example: Building a Java project
Output of
gradle build
> gradle build :compileJava :processResources :classes :jar :assemble :compileTestJava :processTestResources :testClasses :test :check :build BUILD SUCCESSFUL in 0s 6 actionable tasks: 6 executed
Some other useful tasks are:
- clean
- Deletes the
build
directory, removing all built files. - assemble
- Compiles and jars your code, but does not run the unit tests. Other plugins add more artifacts to this task. For example, if you use the War plugin, this task will also build the WAR file for your project.
- check
- Compiles and tests your code. Other plugins add more checks to this task. For example, if you use the
checkstyle
plugin, this task will also run Checkstyle against your source code.
Usually, a Java project will have some dependencies on external JAR files. To reference these JAR files in the project, you need to tell Gradle where to find them. In Gradle, artifacts such as JAR files, are located in a repository. A repository can be used for fetching the dependencies of a project, or for publishing the artifacts of a project, or both. For this example, we will use the public Maven repository:
Let’s add some dependencies. Here, we will declare that our production classes have a compile-time dependency on commons collections, and that our test classes have a compile-time dependency on junit:
Example: Adding dependencies
build.gradle
dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
You can find out more in Dependency Management for Java Projects.
The Java plugin adds a number of properties to your project. These properties have default values which are usually sufficient to get started. It’s easy to change these values if they don’t suit. Let’s look at this for our sample. Here we will specify the version number for our Java project, along with the Java version our source is written in. We also add some attributes to the JAR manifest.
Example: Customization of MANIFEST.MF
build.gradle
sourceCompatibility = 1.7 version = '1.0' jar { manifest { attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version } }
What properties are available?
You can use
gradle properties
to list the properties of a project. This will allow you to see the properties added by the Java plugin, and their default values.
The tasks which the Java plugin adds are regular tasks, exactly the same as if they were declared in the build file. This means you can use any of the mechanisms shown in earlier chapters to customize these tasks. For example, you can set the properties of a task, add behaviour to a task, change the dependencies of a task, or replace a task entirely. In our sample, we will configure the
test
task, which is of type Test
, to add a system property when the tests are executed:
Usually the JAR file needs to be published somewhere. To do this, you need to tell Gradle where to publish the JAR file. In Gradle, artifacts such as JAR files are published to repositories. In our sample, we will publish to a local directory. You can also publish to a remote location, or multiple locations.
Example: Publishing the JAR file
build.gradle
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
To publish the JAR file, run
gradle uploadArchives
.
To create the Eclipse-specific descriptor files, like
.project
, you need to add another plugin to your build file:
Now execute
gradle eclipse
command to generate Eclipse project files. More information about the eclipse
task can be found in The Eclipse Plugins.
Here’s the complete build file for our sample:
Example: Java example - complete build file
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' sourceCompatibility = 1.7 version = '1.0' jar { manifest { attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version } } repositories { mavenCentral() } dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2' testCompile group: 'junit', name: 'junit', version: '4.+' } test { systemProperties 'property': 'value' } uploadArchives { repositories { flatDir { dirs 'repos' } } }Instance initialize/ Anonymous block in java. super in java. Static in java. final in java. String handling. StringBuffer. StringBuilder. StringTokenizer. Exception handling. Multithreading. Input output. Collection framework. How to create jar file. String handling in java with example. Input output (I/O) in java. FileInputStream and FileOutputStream in java. Byte Streams in java with example. DataInputStream and DataOutputStream in java with example. BufferedInputStream and BufferedOutputStream in java. FileReader and FileWriter in java with example. How To Check If A File Exists In Java. Serialization in java with example. Transient in java with example. List all file names from directory java. Read all files from folder java. Filter the files by file types java. Read file content in byte array java. Read file content line by line java. Get file list from a folder filtered by extensions java. Get file uri reference java. Store and read objects from a file java. Create and store property file dynamically java. Store property file as xml file in java. Get file last modified time java. Convert byte array to inputstream java. Convert inputstream to bufferedreader java. Convert byte array to bufferedreader java. Set file permissions in java. Create temporary file in java. Store data into temporary file in java. Delete temporary file in java. Write string content to file java. Write byte content to a file java. Delete file in java program. Rename file in java program. Make a file read only in java. Check if file is writable java. Make a read only file writable in java. Check if a file is hidden in java. Collection framework in java. Collection interfaces in java. Collection interface in java with example. Set interface in java with example. SortedSet interface in java with example. List interface in java with example. Map interface in java with example. Daemon thread in java in java with example. SortedMap interface in java with example. Queue interface in java with example. Deque interface in java with example. Enumeration interface in java with example. Collection classes in java with example. HashSet in java with example. LinkedHashSet in java with example. TreeSet in java with example. ArrayList in java with example. LinkedList in java with example. HashMap in java with example. LinkedHashMap in java with example. TreeMap in java with example. PriorityQueue in java with example. ArrayDeque in java with example. Abstract classes in collection framework in java with example. Sorting in java with example. Comparable interface in java with example. Comparator interface in java with example. Properties class in java with example. Hashtable in java with example. ListIterator interface in java with example. Java data types tutorial. Byte data type example in java. Short data type example in java. Int data type example in java. Long data type example in java. Float data type example in java. Double data type example in java. Boolean data type example in java. Char data type example in java. Java if statement example. Java if else statement example. Java if else if statement example. Java switch statement multiple case. For loop java example. Enhanced for loop java tutorial. While loop java tutorial. Do while loop java tutorial. Break statement in java. Break statement in java with label. Continue statement in java. Java continue statement with label.
No comments:
Post a Comment