将项目打成jar包执行 在liunx上执行 java -xx.jar
一:普通maven 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.hstc</groupId> <artifactId>insert_data_monitor_res</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.4.0</version> </dependency> </dependencies> <build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 将依赖导入lib文件夹
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin>--> <!-- 配置将依赖包一并打入到项目的 jar 包中 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <mainClass>Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Jdbc.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcConn { private String driver; private String url; private String name; private String pass; public Connection getConnection(){ Connection conn=null; try { Class.forName(this.driver);//找到oracle驱动器所在的类 conn=DriverManager.getConnection(this.url, this.name, this.pass); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public void close(){ try { Connection conn=this.getConnection(); Statement stm=this.getPstmt(); if(stm!=null){ stm.close(); } if(conn!=null){ conn.close(); } }catch (Exception e){ e.printStackTrace(); } } public Statement getPstmt() throws SQLException{ Statement stmt=this.getConnection().createStatement(); return stmt; } public ResultSet getRsultSet(String sql) throws SQLException{ ResultSet rs=this.getPstmt().executeQuery(sql); rs.close(); return rs; } public int insert(String sql) throws Exception{ return this.getPstmt().executeUpdate(sql); } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
Main.java
import java.sql.ResultSet; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class Main { public static void main(String[] args) { JdbcConn jdbc=new JdbcConn(); String drive="oracle.jdbc.driver.OracleDriver"; String url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (FAILOVER = on) (ADDRESS = (PROTOCOL = TCP)(HOST = 10.2.194.143)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 10.2.194.144)(PORT = 1521)) ) (CONNECT_DATA = (FAILOVER_MODE = (TYPE = select) (METHOD = basic)) (SERVER = dedicated) (SERVICE_NAME = shipc) ) )"; jdbc.setDriver(drive); jdbc.setUrl(url); jdbc.setName("board"); jdbc.setPass("board321"); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); StringBuffer sb=new StringBuffer(); sb.append("insert into tf_monitor_sms_log"); sb.append("(uuid,content_desc,phone_num,send_flag,sys_time)"); sb.append("values"); sb.append("('"); sb.append(UUID.randomUUID().toString().replaceAll("-", "")); sb.append("','"); sb.append(args[0]); sb.append("','"); sb.append(args[1]); sb.append("',"); sb.append("'0'"); sb.append(",'"); sb.append(sdf.format(new Date())); sb.append("')"); try { System.out.println("==sql==========="); System.out.println(sb.toString()); System.out.println("==sql==========="); int i=jdbc.insert(sb.toString()); if(i>0){ System.out.println("执行结果:=====success"); }else{ System.out.println("执行结果:=====fail"); } }catch (Exception e){ System.out.println("数据插入出现异常"); e.printStackTrace(); }finally { jdbc.close(); } } }
1.将依赖打到另一个文件夹中。
<build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
2.将依赖达到同一个jar文件中。
<build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 配置将依赖包一并打入到项目的 jar 包中 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <mainClass>Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
web项目将依赖加入lib文件夹
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin>