Create a WAR from the java files using Gradle script - java

My directory sturcture is,
C:\Grapher\src\ *.java
C:\Grapher\lib\ *.jar
There are many java files and many jar dependencies. I want to create a tomcat deployable WAR.
Can any one suggest me how to create a WAR using a gradle script? What are the dependencies to be included in Gradle to create a WAR.

Have you read and understood https://docs.gradle.org/current/userguide/war_plugin.html?
The War plugin adds two dependency configurations named providedCompile and providedRuntime. Those two configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive. It is important to note that those provided configurations work transitively. Let's say you add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. Because this is a “provided” configuration, this means that neither of these dependencies will be added to your WAR, even if the commons-codec library is an explicit dependency of your compile configuration. If you don't want this transitive behavior, simply declare your provided dependencies like commons-httpclient:commons-httpclient:3.0#jar.
If you use providedCompile or providedRuntime, those dependencies will not be added to your war archive.
The folder structure you need to have in your project is:
app
|-src
|-main
|-java
|-webapp
In the java folder you add your own packages and classes. In the webapp folder you add web content if your application makes use of such items.

Related

How to include only specific jar from libs into gradle build?

I'm using gradle V4.10.2 and eclipse for building WAR files. I have some common JAR files for all the wars. I need them to use only during compile time and need to exclude them while building into WAR file. Also I don't want to exclude all the JARs file.
I've put all the JARs in libs folder. In build.gradle, I have specified only the JARs to be included. Since the exclude list is bigger then include list of JARs I'm not using exclude
war {
rootSpec.include("**/test.jar")
}
The above results the WAR file without META-INF folder. Hence, webapp is not started. I have tried adding the libs as reference lib in eclipse but that results in compilation error.
Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details
So I have to include all the JARs in libs folder and then need to use only required JARs in the WAR built. How to include only specific JARs into build?
I strongly recommend reading about building Java projects with Gradle and in particular the part about web applications
In short you should add the JARs needed for compilation only to the compileOnly configuration and the JARs needed for compilation and at runtime in the implementation configuration.
Gradle will then use all of them for compilation and only include the ones in implementation when packaging the WAR.
While you can do that by selecting them from your libs folder, I strongly recommend moving to libraries and its metadata sourced from a repository, private or public, so that the dependency management is done for you.

what is the difference between compile and provide scope in maven in context to both at compile time and runtime? [duplicate]

What is the difference between the maven scope compile and provided when artifact is built as a JAR? If it was WAR, I'd understand - the artifact would be included or not in WEB-INF/lib. But in case of a JAR it doesn't matter - dependencies aren't included. They have to be on classpath when their scope is compile or provided. I know that provided dependencies aren't transitive - but is it only one difference?
From the Maven Doc:
compile
This is the default scope, used if none is specified. Compile
dependencies are available in all classpaths of a project.
Furthermore, those dependencies are propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a
container to provide the dependency at runtime. For example, when
building a web application for the Java Enterprise Edition, you would
set the dependency on the Servlet API and related Java EE APIs to
scope provided because the web container provides those classes. This
scope is only available on the compilation and test classpath, and is
not transitive.
Recap:
dependencies are not transitive (as you mentioned)
provided scope is only available on the compilation and test classpath, whereas compile scope is available in all classpaths.
provided dependencies are not packaged
Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory.
Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory.
For a web app, if the app server already provides the JAR (or its functionality), then use "provided" otherwise use "compile".
Here is the reference.
If you're planning to generate a single JAR file with all of its dependencies (the typical xxxx-all.jar), then provided scope matters, because the classes inside this scope won't be package in the resulting JAR.
See maven-assembly-plugin for more information
compile
Make available into class path, don't add this dependency into final jar if it is normal jar; but add this jar into jar if final jar is a single jar (for example, executable jar)
provided
Dependency will be available at run time environment so don't add this dependency in any case; even not in single jar (i.e. executable jar etc)
For a jar file, the difference is in the classpath listed in the MANIFEST.MF file included in the jar if addClassPath is set to true in the maven-jar-plugin configuration. 'compile' dependencies will appear in the manifest, 'provided' dependencies won't.
One of my pet peeves is that these two words should have the same tense. Either compiled and provided, or compile and provide.
If jar file is like executable spring boot jar file then scope of all dependencies must be compile to include all jar files.
But if jar file used in other packages or applications then it does not need to include all dependencies in jar file because these packages or applications can provide other dependencies themselves.

How to use jenkins to take a module maven project and turn it into a .jar

So I'm working on a java project located here. I have it on a jenkins server and I want it to compile all the different modules under one jar. I tried copying what another project did but am unable to produce a jar so I believe it has something to do with how I need to configure jenkins in order for it to make the jar. The jar doesn't need to be executable.
The maven-assembly-plugin can create your jar: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
Your project has 4 JARs and they don't depend on each other.
If NameLayer depends on NameLayer1_8_R1 you have to
add the dependency NameLayer1_8_R1 to the pom.xml of NameLayer and
the maven-assembly-plugin as descrbibed here to create a fat JAR (including all depending JARs).
Do the first step for as many dependencies as you need.

jar as jboss module: dependencies

I have a question about JBoss modules dependencies vs war dependencies.
I have an app.war; its webinf/lib directory contains a myLib.jar only. In this configuration everything works fine.
Now I have to move mylib.jar outside the war, so I put it into a JBoss module and added it to module.xml; I also added it to the deployment-structure.xml file in the war.
In this new configuration my app.war finds succesfully the external myLib.jar but now myLib.jar cannot find its dependencies anymore.
Specifically it doesn't find rowset.jar as I get this exception:
java.lang.NoClassDefFoundError: com/sun/rowset/CachedRowSetImpl
Why can’t myLib.jar see all the libs it used to see when it was deployed inside the war? Do I have to create a JBoss module with myLib dependencies?
Thanks
modules must have their own dependencies explicitly declared in the modules.xml. A Module cannot access jars that are part of a deployment.

Exclude jar files on deploy, in IntelliJ 13

Suppose that I have a small web application (war) which requires 2 jar files; libA.jar and libB.jar. I want to include both of them on build but exclude libB.jar on deploy, since it's already within the classpath of the server.
Is it possible to do that in IntelliJ 13? Note that I am not using any build tools (i.e. Ant, Maven, ...etc).
Try to use scope provided for library libB.jar in tab "Dependencies" of your module.

Categories