我的依赖项定义有什么问题?
What is the wrong with my dependency definition?
我的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>
我的java文件
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");
}
}
我用
编译了它
mvn -e compile
它说
[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
我知道 dependency jar
是 improperly added
因为它抱怨包不存在。我在哪里添加该依赖项以便我的代码能够成功编译和调整?
For more info 我将此站点引用到 运行 这个例子
项目结构
Cassandra
pom.xml
src
main
java
com
example
cassandra
Client.java
我运行从 pom.xml 呈现的 Cassandra 目录中执行这些命令
这是由于您指定的依赖范围所致。不指定范围,默认为compile
.
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.
我的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>
我的java文件
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");
}
}
我用
编译了它 mvn -e compile
它说
[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
我知道 dependency jar
是 improperly added
因为它抱怨包不存在。我在哪里添加该依赖项以便我的代码能够成功编译和调整?
For more info 我将此站点引用到 运行 这个例子
项目结构
Cassandra
pom.xml
src
main
java
com
example
cassandra
Client.java
我运行从 pom.xml 呈现的 Cassandra 目录中执行这些命令
这是由于您指定的依赖范围所致。不指定范围,默认为compile
.
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.