views:

122

answers:

3

Hi, I have a simple project with 3 dependencies but for some reason when i am running from the eclipse run as -> maven install. i don't get the dependencies. not inside the snapshot jar and not outside... any idea? this is my pom:

<?xml version="1.0"?>
<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"&gt;
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>trade</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>trade</name>
  <url>http://maven.apache.org&lt;/url&gt;
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
+6  A: 

(...) I don't get the dependencies. not inside the snapshot jar and not outside... any idea?

Yes: this is just not supposed to happen for a project with a jar packaging. The created jar "only" contains classes from your project, the dependencies are "only" used during compilation and tests execution.

From the Introduction to the Dependency Mechanism:

Dependency Scope

Dependency scope is used to limit the transitivity of a depedency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

  • 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.
  • 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.
  • system
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import (only available in Maven 2.0.9 or later)
    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

...

If you want to build a standalone executable jar including the dependencies, consider using the Maven Assembly Plugin and the pre-defined jar-with-dependencies descriptor. Have a look at these previous answers:

Other options include the maven shade plugin, the onejar-maven-plugin. For simple use cases, I suggest using the assembly plugin.

Pascal Thivent
A: 

First off, make sure you can build the project outside of Eclipse. At a command prompt, try 'mvn install'. That should put your deps beneath $HOME/.m2/repository. If it does, then the problem is with Eclipse.

If your goal is to package your deps with your archive, you'll need to look at the assembly and/or dependency plugins.

Also, I recommend installing m2eclipse.

jcrossley3
A: 

You've chosen a jar style of packaging. Dependencies will therefore not be present inside the package.

When you build the package, and execute the compile phase, Maven would have already downloaded the dependencies into the local Maven repository (usually located in the .m2 directory under your home directory).

If you wish to have the dependencies packaged within the distribution, a EAR/WAR file is a better means, and this applies only to Java EE applications, and not for standalone Java SE applications.

Furthermore, certain dependencies will not be packaged, despite an EAR/WAR packaging scheme involved. This is due to the scopes applied on dependencies. JUnit, which has a test scope, will never get packaged, as it's use is restricted only to test execution.

Vineet Reynolds