-1

My pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cassandra</groupId>
<artifactId>simple-client</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>

 <dependencies>
  <dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>cassandra-driver-core</artifactId>
   <version>2.1.0</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.3.2</version>
      <configuration>
         <mainClass>com.example.cassandra.Client</mainClass>
      </configuration>
    </plugin>
  </plugins>
 </build>
 </project>

My java file

 package com.example.cassandra;

 import com.datastax.driver.core.Cluster;

 public class Client
 {
   // private Cluster cluster;

      public static void main(String a[])
      {
         System.out.println("I am in");
      }
  }

I compiled it using

  mvn -e compile

it says

  [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler- 
     plugin:2.0.2:compile (default-compile) on project simple-client:   
     Compilation failure
  [ERROR] /cassandra/src/main/java/com/example/cassandra/Client.java:[3,31] 
     error: package com.datastax.driver.core does not exist
  [ERROR] -> [Help 1]
     org.apache.maven.lifecycle.LifecycleExecutionException: Failed to  
     execute goal org.apache.maven.plugins:maven-compiler- 
     plugin:2.0.2:compile (default-compile) on project simple-client:   
     Compilation failure
     /cassandra/src/main/java/com/example/cassandra/Client.java:[3,31]  
     error: package com.datastax.driver.core does not exist

I understand that dependency jar is improperly added since it complains package doesnot exist. Where do i add that dependency so that my code will compile and tun successfully?

For more info I am referring this site to run this example

Structure of the project

  Cassandra
     pom.xml
     src
        main
          java
            com
              example
                cassandra
                  Client.java

I am running these commands from Cassandra directory that is where pom.xml presents

Gibbs
  • 21,904
  • 13
  • 74
  • 138

1 Answers1

1

It's due to the dependency scope you specified. By not specifying the scope, the default is compile.

From Maven Documentation:

Dependency scope is used to limit the transitivity of a dependency, 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 section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I read this already. I thought test would be the most suitable choice. Will you please tell me why my code doesn't compile? Because `is only available for the test compilation and execution phases' i see this line in the doc. – Gibbs Feb 26 '15 at 10:49
  • Maven works with a goal and you can specify some scope is dependent on some build goals. Dependencies with test scope will not be included in compile goal and you're doing a compile so the dependency was not available. – Buhake Sindi Feb 26 '15 at 10:51