Gradle create war and jar from same project - java

Im creating a java rest service using spring mvc, gradle and tomcat. I want to create a war from this project to be deployed in tomcat, and I want a jar from all model packages. I managed to create war and it works well. But I don't know to create a jar from my specified classes to be used as a client jar. I know i have to use include('com/a/b/**/model/**') for selecting classes but thats all. This jar i want to be installed in .m2 local repo so it can be used as a dependency for another projects. Below is my build.gradle file:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'maven'
apply from: 'dependencies.gradle'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
mavenLocal()
}
def tomcatHome = 'E:\\apache-tomcat-7.0.62'
//change the name of war because default it is mywar.1.0.0.war
war.archiveName "mywar.war"
//move the war into tomcat webapps folder
task deployToTomcat(type: Copy) {
from war
into "$tomcatHome/webapps"
}

You should split the module into two modules: the API JAR and the WAR. Depend on the API JAR from the WAR.

Related

Gradle war plugin creation of directory structure

Using Eclipse I created a Gradle application which is a library, I want to create a web application and generate a .war file, so I added the war plugin:
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id 'war'
}
On the documentation https://docs.gradle.org/current/userguide/war_plugin.html it says that it creates a directory structure, but it doesn't create for me. Do I have to do something for it to create it or do I have to create it by hand?

When does .m2 folder gets created while using gradle with Java

I am currently working on Java with gralde where the build.properties file looks like this
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'groovy'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
//Artifactory Central Repository
buildscript {
repositories {
maven {
url 'abc'
credentials {
username = ""
password = ""
}
I am totally new to this framework and have the following questions:
Here, I understand that the dependencies are getting downloaded from the maven repository. And wondering if .m2 folder will be created when I execute this. or will this work without creating .m2 local repo.
I read that local repo is needed only for maven, not grade.
So, in this case where Gradle is used to access maven repo to download dependencies, is .m2 folder needed.
When does this .m2 folder gets created?
After the Gradle build is successful, I did check in my {user home} but there are no .m2 created
I did try getting info from Google but didn't get any clarity on this.
confused on how does Gradle uses .m2 while building its project.

How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository?

In my build.gradle, I added the plugin:
apply plugin: 'maven'
Then using gradle install I can copy the resulted jar into the maven repository : ~/.m2/repository
However, my Gradle repository resides in ~/.gradle/caches/modules-2/files-2.1. How can I install the jar into this repository?
What worked for me is gradle install -Dmaven.repo.local=the/path/of/the/folder.
I don't know which IDE you are using but in eclipse you can add a new Run Configuration, in Gradle taks add install and in program arguments -Dmaven.repo.local=the/path/of/the/folder.
If you insist on manipulating the cache, then your best bet is to write a shell script that will manually replace latest JAR in the cache.
The reason is that Gradle does not come with this functionality built-in, as Gradle uses notion of "local cache" in a strict sense, as opposed to "local repository" which is used by Maven.
The difference is that you are never supposed to save files to local cache manually.
To solve your problem the recommended way: Suppose that project A is a dependency of project B. Then you can call publishToMavenLocal command in project A to refresh the depedency. Add mavenLocal() repository in gradle.build of project B, so every time you build project B, Gradle will check the local repository when resolving the dependency A.
mavenLocal() is resolved like this:
In order to make use of a non-standard local maven repository, you can use the following configuration in your build.gradle:
repositories {
maven {
url '/Users/manuelj/apache/maven/repository'
}
}
A build.gradle sample to create a Jar file along with its logback dependencies. using mavenlocale()
apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'
sourceCompatibility = 1.7
target Compatibility = 1.7
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.mkyong.DateUtils'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it :
zipTree(it)
}
}
with jar
}
//Get dependencies from Maven central repository
repositories {
mavenCentral()
}
//Project dependencies
dependencies {
compile 'ch.qos.logback:logback-classic:1.1.2'
}
Reference create a Jar file along with its logback dependencies.

Realm 0.87.5 in Android

I've updated Realm to 0.87.5 and now I'm getting this error on build. Can you guys please help me with this?
Error:Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'. >
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK lib/x86/librealm-jni.so
File1: /Users/[USER_NAME]/.gradle/caches/modules-2/files-2.1/io.realm/realm-android/0.87.5/ab4e1fead1380252dad0e95658e53ea0c113e89c/realm-android-0.87.5.jar
File2: /Users/[USER_NAME]/.android/build-cache/97d752c34cee76117e22adcee3a9c2d132f80273/output/jni
Root Dependency:
App Module Dependency:
Add this to the project level gradle file
classpath "io.realm:realm-gradle-plugin:4.3.3"
And do this in your module level gradle file
apply plugin: 'kotlin-kapt' // if using kotlin
apply plugin: 'realm-android'
But more importantly, make sure you don't have obsolete versions like 0.87.5 in your module level dependencies if you are trying to use a version newer than 0.88.0, such as 4.3.3 in this case.
dependencies {
// compile "io.realm:realm-android:0.87.5" <-- make sure you don't have that
Then you might need to clean + rebuild.
Try with :
classpath "io.realm:realm-gradle-plugin:3.6.0-SNAPSHOT"
in your gradle project dependencies
and check if you're using :
apply plugin: 'realm-android'
in your Module app file
Simply you don't have to compile any dependency for Realm DB, you just have to add two lines,
In your Gradle file at app level
apply plugin: 'realm-android'
In your Gradle file at project level
dependencies {
classpath "io.realm:realm-gradle-plugin:3.5.0"
}

Gradle config to custom plugin

I'm trying to get a better understanding of what is possible to do with gradle plugins. I've done some reading on https://docs.gradle.org/current/userguide/custom_plugins.html, but
I do not feel like it answers my questions.
Right now we have several projects that are built into ear files with the ear-plugin.
However, for all our ear-projects we have a bunch of other config that we at the moment have in a company_ear.gradle file
that we apply in the build.gradle file of our ear-projects.
example of our company_ear.gradle:
allprojects {
tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
}
apply plugin: 'ear'
ear {
appDirName 'application'
rename('([^-]*)(.*)(.war)', '$1$3')
rename('([^-]*)(.*)(.jar)', '$1$3')
}
task devDeploy (type: Copy, dependsOn: assemble) {
//code to do local deploy of ear
}
// More custom stuff that should apply to all ear-projects
So my question is, is it possible to move this to a custom gradle plugin so I can just do "apply plugin: company-ear"? I understand how to do the "devDeploy" task
in a plugin but I can not understand how I would fix the allProjects, apply plugin: 'ear', and ear-block in a custom plugin.

Categories