Saturday 7 April 2018

Multi-project Java build

Multi-project Java build

Now let’s look at a typical multi-project build. Below is the layout for the project:

Example: Multi-project build - hierarchical layout
Build layout
multiproject/
  api/
  services/webservice/
  shared/
  services/shared/
Note: The code for this example can be found at samples/java/multiproject in the ‘-all’ distribution of Gradle.
Here we have four projects. Project api produces a JAR file which is shipped to the client to provide them a Java client for your XML webservice. Project webservice is a webapp which returns XML. Project shared contains code used both by api and webservice. Project services/shared has code that depends on the shared project.

Defining a multi-project build

To define a multi-project build, you need to create a settings file. The settings file lives in the root directory of the source tree, and specifies which projects to include in the build. It must be called settings.gradle. For this example, we are using a simple hierarchical layout. Here is the corresponding settings file:
Example: Multi-project build - settings.gradle file
settings.gradle
include "shared", "api", "services:webservice", "services:shared"
You can find out more about the settings file in Authoring Multi-Project Builds.

Common configuration

For most multi-project builds, there is some configuration which is common to all projects. In our sample, we will define this common configuration in the root project, using a technique called configuration injection. Here, the root project is like a container and the subprojects method iterates over the elements of this container - the projects in this instance - and injects the specified configuration. This way we can easily define the manifest content for all archives, and some common dependencies:
Example: Multi-project build - common configuration
build.gradle
subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse-wtp'

    repositories {
       mavenCentral()
    }

    dependencies {
        testCompile 'junit:junit:4.12'
    }

    version = '1.0'

    jar {
        manifest.attributes provider: 'gradle'
    }
}
Notice that our sample applies the Java plugin to each subproject. This means the tasks and configuration properties we have seen in the previous section are available in each subproject. So, you can compile, test, and JAR all the projects by running gradle buildfrom the root project directory.
Also note that these plugins are only applied within the subprojects section, not at the root level, so the root build will not expect to find Java source files in the root project, only in the subprojects.

Dependencies between projects

You can add dependencies between projects in the same build, so that, for example, the JAR file of one project is used to compile another project. In the api build file we will add a dependency on the shared project. Due to this dependency, Gradle will ensure that project shared always gets built before project api.
Example: Multi-project build - dependencies between projects
api/build.gradle
dependencies {
    compile project(':shared')
}
See the section called “Disabling the build of dependency projects” for how to disable this functionality.

Creating a distribution

We also add a distribution, that gets shipped to the client:
Example: Multi-project build - distribution file
api/build.gradle
task dist(type: Zip) {
    dependsOn spiJar
    from 'src/dist'
    into('libs') {
        from spiJar.archivePath
        from configurations.runtime
    }
}

artifacts {
   archives dist
}
Program to print alphabets in java. Java Arithmetic Operators Example. Armstrong number program in java. Even odd program in java. Factorial program in java . Factorial program recursion in java. Fibonacci series program in java. Palindrome number program in java. Prime number program in java. Java swap two numbers without using third variable. Swap numbers program in java. Reverse number program in java. Calculate circle area using java. Calculate circle perimeter in java. Calculate rectangle area using java. Calculate rectangle perimeter java. Leap year java program code. List Even Numbers in Java. List Odd Numbers in Java. Floyd triangle in java example. Generate pyramid for a given number. Generate pyramid triangle in java. Servlets tutorial. Servlet overview. Life cycle of a servlet. Servlet interface. GenericServlet class. HttpServlet class. web.xml file. welcome-file-list. load-on-startup. RequestDispacher interface. sendRedirect. Init parameters. Context parameters. Annotation example. Session management. Cookie in servlet. Hidden field. URL rewriting. HttpSession. Servlet filter. FilterConfig interface. JSP tutorial. JSP overview. JSP Hello World. JSP Scriptlet tag. JSP Declaration tag. JSP Expression tag. JSP comment tag. JSP directives. JSP implicit objects. JSP action tags. Exception handling. Expression Language. JSTL. JSTL Core Tags. JSTL Formatting Tags. JSTL Functions. Custom tags. Custom tag with attributes. Struts tutorial. Struts 2 overview. Struts 2 Architecture. Configuration Files. Action in struts 2. Hello World example. Multi-configuration file. OGNL. Value Stack. Interceptors. execAndWait interceptor. Custom interceptor. Result type. Redirect result type. Validation framework. Built-in validators. Zero Configuration by convention approach. Zero Configuration by annotation approach. DispatchAction Functionality. Dynamic method invocation. UI tags. Control tags. Data tags. Tiles integration. Spring tutorial. Spring framework. Spring architecture. Spring ioc container. Spring bean. Spring bean scopes. Spring bean life cycle. Spring callback methods. Spring hello world. Spring bean definition inheritance. Spring bean definition template. Spring dependency injection. Spring constructor based injection. Constructor injection type ambiguities. Setter based dependency injection.

No comments:

Post a Comment