5

E.g. if I make the BukkitApi jar a dependency for a maven project with depenecy scope set to provided,compile,system, runtime or test

In which scopes will the bukkitAPI be included in the compiled output?

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
  • 1
    what compiled output ? Which phase are you talking about ? e.g if you set the scope to test and nothing else, the test compiled output will obviously use the dependency. Which does not mean that it will be "included" in anything, as long as you don't explicitely include dependencies in any output (for exemple, output of the `package` phase) – Erwan Queffélec Nov 23 '12 at 14:33
  • I honestly don't know which phase, I'm entirely new to maven, and someone on an IRC channel confused me and I suspected they were wrong, I googled to no avail so I asked stackoverflow. – Ryan Leach Nov 23 '12 at 14:56

2 Answers2

8

Short version: By default, maven output (in the default target directory) does not include anything except the compiled code for the current project/module. That is, nothing from dependencies.

Long(er) version: with default jar packaging and no custom phase configuration. here is how maven behaves on a java project:

  1. the compile phase : the .java files in the src/main/java/ directory get compiled to .classes files in the target directory. Dependencies for the compile scope get downloaded to your local repository.
  2. the package phase : same as 1, plus you'll get a jar file in the target directory
  3. the install phase : same as 2 you'll get a jar file in your local repository.

So, .jar files from dependencies are not included in anything by default !

So, how do I include dependencies in my "output", and what does thoses scopes mean ?

Now, using, for exemple, the assembly plugin to include dependencies in the output of the package phase (see Including dependencies in a jar with Maven), you'll normally get this default behavior:

  • provided : not included
  • compile (default) : included
  • system : not included
  • runtime : included
  • test : not included

Checkout this link for reference.

EDIT: Just try out this pom with the different scope values on guice, and you'll see that dependencies are included in the fake-1.0-SNAPSHOT-jar-with-dependencies.jar when scope is compile and runtime (this example does not need any source files)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.linagora</groupId>
    <artifactId>fake</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>fake</name>
    <dependencies>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>2.0</version>
            <scope>compile</scope>
            <!-- system scope needs 'systemPath' attribute as well
                <systemPath>/path/to/guice/guice-3.0.jar</systemPath>
                <scope>system</scope>
            -->
            <!-- <scope>runtime</scope> -->
            <!-- <scope>test</scope> -->
            <!-- <scope>provided</scope> -->

        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Community
  • 1
  • 1
Erwan Queffélec
  • 716
  • 1
  • 6
  • 20
  • people are claiming that compile does NOT include the dependencys in the assembled output, giving [worldguard]{https://github.com/sk89q/worldguard/blob/master/src/main/assembly/default.xml} as an example, its [pom]{https://github.com/sk89q/worldguard/blob/master/pom.xml} is it doing something non default? – Ryan Leach Nov 23 '12 at 15:17
  • You can see what the "default" is with the pom.file above. Just unzip the `fake-1.0-SNAPSHOT-jar-with-dependencies.jar` with the compile scope and see that guice files are included. – Erwan Queffélec Nov 23 '12 at 15:37
  • 1
    Had a look at your example. They are not using the 'jar-with-dependencies' configuration so it is, well, not "the-same-defaults-as-mine". Assembly itself is not a default in maven, it is a plugin, with custom configuration - thus, it is quite difficult to tell what is "default" and what isn't when you are already overriding the defaults ! I used `jar-with-dependencies` assembly to illustrate the effect of dependency `scope`, and here you are... – Erwan Queffélec Nov 23 '12 at 15:43
3

That's not how Maven works. The dependencies just specify the classpath (for compilation, run time, testing). But the dependencies are not included in the output by default. You will have to ship all the dependency jars (at least the ones with scope compile and runtime).

Have a look at the dependency plugin. It provides goals to copy the dependencies.

To create a bundle for shipment, have a look at the assembly plugin (e.g. to create a zip file). It even provides a way to create an all-in-one jar, if that is what you're after.

Puce
  • 37,247
  • 13
  • 80
  • 152