我想在我的linux终端中编译一个Netbeans Maven - Java应用程序。
这是因为我想使用Git CVS并能够在独立于IDE的服务器中编译项目。
但我很难理解依赖关系是如何工作的,或者是我的想法。
我使用简单的java应用程序代码连接到mysql数据库并获取一些信息。
我正在使用此代码:
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
try (Connection connect = DriverManager.getConnection("jdbc:mysql://192.168.2.21/altadb?" + "user=marco&password=Marco19")) {
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery("select idCuentaConcentradora, noCliente, nombreCliente, noCuenta, nombreCuenta from altadb.catCuentaConcentradora");
while (resultSet.next()) {
int idCuentaConcentradora = resultSet.getInt("idCuentaConcentradora");
String noCliente = resultSet.getString("noCliente");
String nombreCliente = resultSet.getString("nombreCliente");
String noCuenta = resultSet.getString("noCuenta");
String nombreCuenta = resultSet.getString("nombreCuenta");
System.out.println("idCuentaConcentradora: " + idCuentaConcentradora);
System.out.println("noCliente: " + noCliente);
System.out.println("nombreCliente: " + nombreCliente);
System.out.println("noCuenta: " + noCuenta);
System.out.println("nombreCuenta: " + nombreCuenta);
System.out.println("---------------------------------------------------");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
然后将mysql-connector-java添加到项目依赖项中,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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alta.MySQLAccess_Mvn</groupId>
<artifactId>MySQLAccess_Mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
如果我使用netbeans IDE和clean并构建然后运行项目,则没有错误,并且从数据库中获取信息。
如果我尝试从终端做同样的事情,我会收到错误:
[marko@mark-laptop MySQLAccess_Mvn]$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MySQLAccess_Mvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MySQLAccess_Mvn ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MySQLAccess_Mvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ MySQLAccess_Mvn ---
[INFO] Compiling 1 source file to /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MySQLAccess_Mvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/marko/NetBeansProjects/MySQLAccess_Mvn/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ MySQLAccess_Mvn ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MySQLAccess_Mvn ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MySQLAccess_Mvn ---
[INFO] Building jar: /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MySQLAccess_Mvn ---
[INFO] Installing /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar to /home/marko/.m2/repository/com/alta/MySQLAccess_Mvn/MySQLAccess_Mvn/1.0-SNAPSHOT/MySQLAccess_Mvn-1.0-SNAPSHOT.jar
[INFO] Installing /home/marko/NetBeansProjects/MySQLAccess_Mvn/pom.xml to /home/marko/.m2/repository/com/alta/MySQLAccess_Mvn/MySQLAccess_Mvn/1.0-SNAPSHOT/MySQLAccess_Mvn-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.866 s
[INFO] Finished at: 2014-06-18T18:46:28-06:00
[INFO] Final Memory: 12M/144M
[INFO] ------------------------------------------------------------------------
[marko@mark-laptop MySQLAccess_Mvn]$ java -cp target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar com.marco.mysql.first.test.test
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.marco.mysql.first.test.test.main(test.java:23)
因为在pom.xml中声明了依赖项,所以mysql驱动程序已经被添加到项目库中了......但它没有。
我想从终端,从构建到运行,从Netbeans获得相同的输出。
我不知道该怎么做。
答案 0 :(得分:0)
如果您已将maven添加到系统路径,那么您可以使用提示符上的maven命令简单地编译它:
mvm clean install