maven 项目示例

maven 项目

xml 复制代码
<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">
  <properties>
    <maven.compiler.source>7</maven.compiler.source>
    <maven.compiler.target>7</maven.compiler.target>
  </properties>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-project2</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-project2</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.26</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- 使用 maven-dependency-plugin 插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <!-- 将依赖复制到 JAR 文件的 lib 目录中 -->
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- 设置 maven-jar-plugin 生成 JAR 文件的 MANIFEST.MF 文件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <!-- 指定类路径为 lib 目录下的所有依赖 -->
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.example.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>


</project>

代码

java 复制代码
package com.example;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("====");
            Connection connect=DriverManager.getConnection("jdbc:mysql://10.110.17.233/db","hq","123");
            System.out.println("====");
            Statement stmt=connect.createStatement();
            ResultSet rs=stmt.executeQuery("select * from data");
            while(rs.next())
              System.out.println(rs.getString(1)+"  "+rs.getInt(2)+"  "+rs.getString(3));
            connect.close();
        }catch(Exception e){ System.out.println(e);}

        
    }
}

执行命令`

shell 复制代码
mvn  clean
mvn compile
mvn  package
java -jar /home/hq/java/my-project2/target/my-project2-1.0-SNAPSHOT.jar
相关推荐
星空2 小时前
金蝶苍穹build.gradle配置
java·build.gradle
步行cgn5 小时前
Spring 基于 XML 的自动装配:byName 详解
java·后端·spring
大帅点兵6 小时前
Apache Ant 1.9.9 完整讲解
java
LuTshoes6 小时前
spring ai 实战 手搓 PlaneExecuteAgent
java·人工智能·spring·ai
这料鬼有毒6 小时前
二刷hot100-73.矩阵置零
java
caoerzhong6 小时前
JeeWMS 开源仓库管理系统二次开发与接口对接实战:Java WMS 如何与 ERP、MES 和自动化设备打通
java·开源
长谷深风1116 小时前
评测AI Agent:三种裁判各司其职
java·大数据·开发语言·人工智能·ai agent
边境悍匪6 小时前
蜗牛学苑 Java 智能体学习 Day42|项目周开发技术汇总 1 思维导图复盘
java·开发语言·spring boot·学习·spring
槑有烦恼7 小时前
Java 字符串 & List集合 高频易错真题复盘(含全部错题+修正)
java
语戚7 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp