Sonatype's Maven:The Complete Reference says that a compile scoped dependency is on all classpaths and is packaged with the artifact.
Compile is the default scope; all dependencies are compile-scoped if a
scope is not supplied. compile dependencies are available in all
classpaths, and they are packaged.
I can't see that they are packaged though . . . doesn't this mean that they should be contained in the jar file? If not, what does it mean?
You are correct. Compiled scope dependencies does not get packaged with the output jar. (with JAR plugin). I think the 'package' refers to the end product (binary executable).
I came across this stackOverflow thread (How can I create an executable jar with dependencies using Maven?). Here they are packaging all the dependencies to build a executable out of Main class. In that case you need all the compile time dependencies in your packaged executable. (since JAVA lazy loads it is not a must, but preferable to have all the compile time dependencies)
Related
I have a maven project that should be obfuscated by proguard plugin.
This works fine when my dependency jar is not obfuscated.
But the result is that the dependency jar (also owned by me) is included in my obfuscated app not obfuscated. The opposite when i include the already obfuscated jar in my project i get compile errors for not finding packages and classes.
How can i obfuscate both my app and my dependency jar?
I was learning maven scope and I encountered a doubt.
If scope of all the dependencies in a project , say A , is compile , then they will be present in its jar too. So, it is said, for any other project , say B, that depends on this project A , will get transitive dependencies too of A. But they are already present in the jar of project A ? Why download them again ?
They are not "present in the jar". Transitive dependencies of a jar are not bundled into the jar, unless you explicitly build a fat jar, e.g. with the assembly plugin or shade plugin.
Fat jars, though, are not meant to be dependencies of other artifacts, they are only meant to be run standalone.
For ears and wars, the situation is different (standard is to bundle everything), but wars and ears do not serve as libraries that you depend on.
How to write a gradle script so that the application jar should be packed without dependency classes similar to maven jar package
The application JAR is always without dependencies, except you use special plugins to create a "fat" JAR where the dependencies are included. If you ask how to set up a Gradle build at all, you should start reading the Users Guide.
If you are trying to package a jar from your Android app or library:
I ran into this question because gradle would include 3rd party libraries into my jar when running gradle assembleRelease.
The contents of the jar then looked like this:
com/android/...
/myCompany/...
For some reason this did not happen when building for debug. Finally I found that changing:
compile 'com.android.support:recyclerview-v7:23.0.0'
to
provided 'com.android.support:recyclerview-v7:23.0.0'
would not include the 3rd party libs ...
I have a Maven project with one java file and that uses the Maven shade plugin to create an uber jar. My goal is to create an executable jar as small as possible. I decided to use the minimizeJar parameter in the plugin to make my jar smaller. Is the parameter only putting in the dependencies needed to run or the ones to compile or both?
The answer is further up the page that you link to:
shade:shade
Full name: org.apache.maven.plugins:maven-shade-plugin:2.3:shade
Description: Mojo that performs shading delegating to the Shader component.
Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: runtime.
The goal is thread-safe and supports parallel builds.
Binds by default to the lifecycle phase: package.
So the artifacts included will be runtime (and therefore compile time as well).
EDIT: For a full explanation of scopes, please see Introduction to the dependency mechanism - Dependency Scope.
In maven, when you do dependency resolution, it uses the notion of scopes - the three most important are (from that page):
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.
runtime - This scope indicates that the dependency is not required
for compilation, but is for execution. It is in the runtime and test
classpaths, but not the compile classpath.
test - This scope
indicates that the dependency is not required for normal use of the
application, and is only available for the test compilation and
execution phases.
So when you compile the sources under src/main/java, you will use dependencies with compile scope. When you run your application, you will use the dependencies with compile or runtime scope. When you compile your tests (under src/test/java), you will use compile and test scopes. When you run your tests with surefire, you will use the dependencies with the compile, test and runtime scopes.
This means that minimizeJar will contain the dependencies which are compile and runtime scope.
Trying to avoid the use of jargon, so that I don't get misinterpreted.
Here is the scenario, My project requires a jar in order to get compiled(let say x.jar). My project get once compiled gets converted into a WAR file, which gets deployed somewhere.
Now I want x.jar just to be there for my project to compile and it should not be packed(or part of) inside WAR file.
How can I do this in Maven ? should I used dependency scope as "provided"
You are right, as stated in the Maven FAQs, the scope to use is provided,
How do I prevent including JARs in WEB-INF/lib? I need a "compile only" scope!
The scope you should use for this is provided. This indicates to Maven that the dependency will be provided at run time by its container or the JDK, for example.
Dependencies with this scope will not be passed on transitively, nor will they be bundled in an package such as a WAR, or included in the runtime classpath.
To quickly try it out, you can use
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
to generate a "toy webapp" project, add a dependency to your project and set it to <scope>provided</scope>.