首先给大家看一下目录 Speed是我的主程序
pom.xml所有的配置(主要看一下 build里面两行注释)
一开始我没有配置引入依赖,打完的jar包运行却报没有log4j的那个类,所以一定要将引入的依赖一起打成jar包。可以直接复制build里面的所有内容,把主程序的位置做修改即可。
<?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>topsec.com</groupId>
<artifactId>NetSpeed</artifactId>
<version>1.0-SNAPSHOT</version>
<name>NetSpeed</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId> maven-assembly-plugin </artifactId>
<configuration>
<descriptorRefs>
<!-- 配置了这个才会把引入的依赖一起打到jar包中-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 这里是指定主程序所在为位置 -->
<mainClass>topsec.com.Speed</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
使用maven,先双击clean,再双击package,等待运行完成 。
最后会生成两个jar包,把第二个jar包放到服务器上去运行即可。
如果不知道如何在linux上如何运行jar包可以参考下文。