Maven打包Jar
我举个例子,例如很多年轻人跟我一样喜欢刷访问量(呸呸呸),所以你懂的吧,我们需要把JAVA写好的代码打包起来,然后蹭蹭蹭的刷,一篇文章两个知识点
1.学习Maven打包Jar
2.学习Http发送Get请求
简述
首先了解一下Maven的一些基础使用
我们发现,上面有许许多多的一些看不懂的功能,但是实际上我们可以通过字面意思去理解
- Maven clean:清除那些target包
- Maven install:执行完整的一个生命周期流程
- Maven package:打包,例如我们用的jar
- Maven test:测试包
- …
所以大概是这样,比较粗浅,但是也很直接,我们的需求是打jar包,所以我们会用到Maven package,但是在上面提到了一个叫做Maven Install的功能,是执行完整的一个生命周期的,并且还会把打好的包装到本地仓库去,回到重点,下面开始玩起来
源程序
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
public class RunMyBook {
public static String doGet(String url) throws IOException {
String body = "";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpGet.releaseConnection();
return body;
}
public static void getUrl() throws IOException, InterruptedException {
while (true) {
String url_1 = "https://blog.csdn.net/weixin_48518621/article/details/120603116?spm=1001.2014.3001.5501";
String url_2 = "https://blog.csdn.net/weixin_48518621/article/details/120445198?spm=1001.2014.3001.5501";
String url_3 = "https://blog.csdn.net/weixin_48518621/article/details/120890975?spm=1001.2014.3001.5501";
doGet(url_1);
doGet(url_2);
doGet(url_3);
Thread.sleep(30000);
}
}
public static void main(String[] args) throws IOException, InterruptedException {
getUrl();
}
}
pom
pom文件整起来
<?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.ljm</groupId>
<artifactId>1_csdn_happy</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<excludes>
<exclude>*.bat</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-command</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>*.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.some.package.some.class.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
打包与执行
执行
java -cp .\1_csdn_happy-1.0-SNAPSHOT.jar com.ljm.RunMyBook
是不是很好玩
over
|