pages tagged Gradle http://meng6net.localhost/tag/Gradle/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Tue, 16 May 2017 23:59:39 +0000 Installing and configuring Gradle http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/ computing configuration documentation gradle installation java note software tool tutorial Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p><a href="http://spring.io/guides/gs/gradle/">Spring Framework guides ''Building Java Projects with Gradle'': http://spring.io/guides/gs/gradle/</a> is a good reference for this topic.</p> <h2>Install Gradle</h2> <p>On macOS, install with Homebrew</p> <pre><code>$ brew install gradle ==&gt; Downloading https://services.gradle.org/distributions/gradle-1.12-bin.zip ######################################################################## 100.0% /usr/local/Cellar/gradle/1.12: 155 files, 44M, built in 106 seconds$ brew install gradle ==&gt; Downloading https://services.gradle.org/distributions/gradle-1.12-bin.zip ######################################################################## 100.0% /usr/local/Cellar/gradle/1.12: 155 files, 44M, built in 106 seconds </code></pre> <p>Verify it's installed successfully:</p> <pre><code>$ which gradle /usr/local/bin/gradle $ gradle -version ------------------------------------------------------------ Gradle 2.0 ------------------------------------------------------------ Build time: 2014-07-01 07:45:34 UTC Build number: none Revision: b6ead6fa452dfdadec484059191eb641d817226c Groovy: 2.3.3 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.6.0_65 (Apple Inc. 20.65-b04-462) OS: macOS 10.9.4 x86_64 </code></pre> <h2>Create Gradle-managed Java projects in IntelliJ IDEA</h2> <p>Really, there are just two easy steps:</p> <p>Create New Project, and then</p> <p><a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_1.png"> <img src= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_1.png" width="900" height="392.037470725995" class="img" /></a> ☝ step 1: pick Gradle, check <a href= "http://www.jetbrains.com/idea/webhelp/importing-project-from-gradle-model.html"> "Use auto-import"</a>, check "Create directories for empty content roots automaticaly", pick JDK version,</p> <p><a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_2.png"> <img src= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_2.png" width="900" height="624.324324324324" class="img" /></a> ☝ step 2: specify settings of the project.</p> <p>Alternatively, one can create a Java project using Gradle from UNIX command line and optionally import it later into an IDE such as IntelliJ IDEA or Eclipse.</p> <h2>Create a Java project using Gradle</h2> <p>Create a project using Gradle:</p> <pre><code>$ mkdir gradlehelloworld $ cd gradlehelloworld $ gradle init --type java-library :wrapper :init BUILD SUCCESSFUL Total time: 4.974 secs </code></pre> <p>Alternatively, use <code>gradle buildInit --type java-library</code> to create it.</p> <p>It automatically creates the project structure including directories and template files:</p> <pre><code>$ tree . ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main │ └── java │ └── Library.java └── test └── java └── LibraryTest.java 7 directories, 8 files </code></pre> <h2>Make a minimal and slightly non-trivial Java program including tests and Gradle build script</h2> <p>I'll rename <code>Library</code> to <code>Main</code> in the file names and source code, and create a toy Java project with a test:</p> <p><code>Main.java</code>:</p> <pre><code>package gradlehelloworld; import com.google.common.base.Strings; public class Main { public static void main(String[] args) { System.out.println(getGreeting("World")); System.out.println("My name is " + System.getProperty("gradlehelloworld.name")); } static String getGreeting(String name) { return Strings.repeat(String.format("Hello %s!\n", name), 2); } } </code></pre> <p><code>MainTest.java</code>:</p> <pre><code>package gradlehelloworld; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.junit.Test; public class MainTest { @Test public void getGreeting() { Main classUnderTest = new Main(); assertThat(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n")); } } </code></pre> <p>Edit the Gradle build script <code>gradlehelloworld/build.gradle</code>:</p> <pre><code>// Apply the java plugin to add support for Java apply plugin: 'java' apply plugin: 'application' sourceCompatibility = '1.6' mainClassName = 'gradlehelloworld.Main' // In this section you declare where to find the dependencies of your project repositories { // Use 'maven central' for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. mavenCentral() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time // compile 'org.slf4j:slf4j-api:1.7.5' compile 'com.google.guava:guava:17.0' // Declare the dependency for your favourite test framework you want to use in your tests. testCompile 'junit:junit:4.11' } run { systemProperty 'gradlehelloworld.name', 'FooBar' } </code></pre> <p>I added these code in <a href= "https://github.com/lumeng/repogit-java-memory-use-experiment">a repository on GitHub</a>. The actual code in the repository is slightly different from the above as I have developed new content and played with variations in it.</p> <h2>Run/build Java project using Gradle</h2> <p>Let Gradle download dependency Guava from Maven Central repository, compile <code>Main.java</code> program, set <code>gradlehelloworld.name</code> to <code>"FooBar"</code>, and run it with Guava on the Java classpath:</p> <pre><code>$ gradle run :compileJava Download http://repo1.maven.org/maven2/com/google/guava/guava/17.0/guava-17.0.pom Download http://repo1.maven.org/maven2/com/google/guava/guava-parent/17.0/guava-parent-17.0.pom Download http://repo1.maven.org/maven2/com/google/guava/guava/17.0/guava-17.0.jar :compileJava FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. &gt; invalid source release: 1.8 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 14.309 secs </code></pre> <p>If I change</p> <pre><code>sourceCompatibility = '1.8' </code></pre> <p>to</p> <pre><code>sourceCompatibility = '1.6' </code></pre> <p>it then works:</p> <pre><code>$ gradle run :compileJava :processResources UP-TO-DATE :classes :run Hello World! Hello World! My name is FooBar BUILD SUCCESSFUL Total time: 6.13 secs </code></pre> <p>To use <code>sourceCompatibility = '1.8'</code>, it might need to set <code>JAVA_HOME</code> environment variable to value of <code>/usr/libexec/java_home -v 1.8</code>.</p> <p>Do a full build including running tests:</p> <pre><code>$ gradle build :compileJava :processResources UP-TO-DATE :classes :jar UP-TO-DATE :assemble UP-TO-DATE :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build UP-TO-DATE BUILD SUCCESSFUL Total time: 6.489 secs </code></pre> <p>Check the test report at</p> <pre><code>./build/reports/tests/index.html </code></pre> <p>Remarks:</p> <ul> <li>Note it uses <code>assertThat</code> instead of <code>assertTrue</code></li> </ul> <p><code>assertTrue(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n"));</code></p> <p><code>assertThat(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n"));</code></p> <ul> <li>Note the use of <code>import static</code> (c.f. <a href= "http://stackoverflow.com/questions/6772647/junit-method-not-found"> a related stackoverflow.com thread</a>.)</li> </ul> <p>At this point your project structure looks like:</p> <pre><code>$ tree . ├── build │ ├── classes │ │ ├── main │ │ │ ├── gradle_hello_world │ │ │ │ └── Main.class │ │ │ └── gradlehelloworld │ │ │ └── Main.class │ │ └── test │ │ └── gradlehelloworld │ │ └── MainTest.class │ ├── dependency-cache │ ├── libs │ │ ├── gradle_hello_world.jar │ │ └── gradlehelloworld.jar │ ├── reports │ │ └── tests │ │ ├── classes │ │ │ ├── css │ │ │ │ ├── base-style.css │ │ │ │ └── style.css │ │ │ ├── gradlehelloworld.MainTest.html │ │ │ ├── htc │ │ │ │ └── css3-pie-1.0beta3.htc │ │ │ └── js │ │ │ └── report.js │ │ ├── css │ │ │ ├── base-style.css │ │ │ └── style.css │ │ ├── htc │ │ │ └── css3-pie-1.0beta3.htc │ │ ├── index.html │ │ ├── js │ │ │ └── report.js │ │ └── packages │ │ ├── css │ │ │ ├── base-style.css │ │ │ └── style.css │ │ ├── gradlehelloworld.html │ │ ├── htc │ │ │ └── css3-pie-1.0beta3.htc │ │ └── js │ │ └── report.js │ ├── test-results │ │ ├── TEST-gradlehelloworld.MainTest.xml │ │ └── binary │ │ └── test │ │ ├── output.bin │ │ ├── output.bin.idx │ │ └── results.bin │ └── tmp │ └── jar │ └── MANIFEST.MF ├── build.gradle ├── build.gradle~ ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── settings.gradle~ └── src ├── main │ └── java │ ├── Main.java │ └── Main.java~ └── test └── java ├── MainTest.java └── MainTest.java~ 34 directories, 37 files </code></pre> <p>The <code>build/</code> sub-directory contains the build, Java doc pages, JUnit test reports, etc.</p> <h2>Import the project into IntelliJ IDEA</h2> <p><a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a> is a popular IDE for Java. One can import the Gradle project created above to the IDE.</p> <ol> <li>Import the Gradle project into IntelliJ IDEA, by following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/importing-project-from-gradle-model.html"> "Importing Project from Gradle Model"</a>. For project settings, I just chose "Use default gradle wrapper (recommended)", which seems to cause a predefined set of tasks to be created in the Gradle tool window such as "compile", "build", "clean", etc.</li> <li>It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to <code>build.gradle</code>. Namely, download the <code>com.google.guava</code> library by following <a href= "http://www.jetbrains.com/idea/webhelp/downloading-libraries-from-maven-repositories.html"> the instruction in documentation</a> -- I picked <code>com.google.guava:guava:17</code> (format is <code>package:artifact:version</code>) to download. (Warning: in IntelliJ IDEA 13.1, I found that one sometimes need to hit the magnifier icon multiple times for it to find a specific artifact from Maven. So don't get confused if it does not find it the first time.)</li> <li>Add the library as a "project library" (by following the automatically prompted window after step 2) or as a global library, by following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/configuring-project-and-global-libraries.html"> "Configuring Project and Global Libraries"</a>.</li> <li>Specify that your Java module depends on the <code>com.google.guava</code> package, following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html"> "Configuring Module Dependencies and Libraries"</a>;</li> <li>If the IDE still reports "Cannot resolve symbol 'google'" and highlights 'google' as red in <code>import com.google.common.base.Strings</code>, one may try using <code>File | Invalidate Caches / Restart …</code> to force the rebuilding of syntax highlighting in the editor window.</li> <li>One can then open the Gradle tool window, and run the various Gradle tasks such as "compile", "build", and "clean".</li> </ol> <h2>Run Gradle tasks from IntelliJ IDEA Gradle Tool Window</h2> <p>The official <a href= "http://www.jetbrains.com/idea/webhelp/gradle-tool-window.html">IntelliJ IDEA documentation ''Gradle Tool Window''</a> to get an introduction. Hitting <code>build</code> under "All tasks" is equivalent to running <code>gradle build</code> at UNIX command line. It generates <code>/build</code> under project root path. Note JUnit test report is at <code>/build/reports/tests/</code>; Similarly, <code>compileJava</code> compiles Java and <code>javadoc</code> creates Javadoc pages in <code>/build/javadoc/</code>. See <a href= "http://www.gradle.org/docs/current/userguide/java_plugin.html">Gradle documentation</a> for the list of Gradle tasks.</p> <h2>References</h2> <ul> <li><a href="http://spring.io/guides/gs/gradle/">Spring Framework guides ''Building Java Projects with Gradle'': http://spring.io/guides/gs/gradle/</a></li> <li> http://www.gradle.org/docs/1.12/userguide/tutorial_java_projects.html</li> </ul> 筆記:用 Gradle 構建 Java 項目 http://meng6net.localhost/zh/blog/note_on_setting_up_java_projects_using_gradle/ http://meng6net.localhost/zh/blog/note_on_setting_up_java_projects_using_gradle/ Gradle Java 工具 筆記 網誌 編程 Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p><a href="https://en.wikipedia.org/wiki/Gradle">Gradle</a> (http://www.gradle.org/) 是一個較新的軟件項目構建自動化工具,在 Java 界很流行。我最近剛 入門。實踐中順手寫了這個筆記,記錄用 Gradle 配置和構建一個簡單 Java 項 目幷包括執行測試的過程: 〈 <a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/"> Installing and configuring Gradle</a> 〉.</p>