Java 构建工具主要有三个,Ant、Maven 和 Gradle,本文关注于在 Eclipse 中如何使用 Maven。
用 Maven 构建一个 Java Project
基本用法
新建一个 Maven Project。(当前的 Eclipse 本身已经集成了 Maven 的功能,无需单独下载 Maven。)
在 Select an Archetype 页面保持默认即可。如果是Web Project,需要选择 maven-archetype-webapp。
指定 Group Id 和 Artifact Id。二者组合起来对应了 Package,一般来讲,所有代码均要放在这个 Package 中。
点击 Finish 即可完成 Maven 项目的创建。
<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>cn.edu.mm</groupId>
<artifactId>hello-mm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-mm</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.edu.mm.hello_mm.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
注:
需要指明函数入口
如果引入了 jar 包,需要进行特殊的处理
详见 plugins 部分,细节不是很清楚,不过这么做是其中的一种实现方式。
还可以把所有的 jar 包打包到一个文件中。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cn.edu.mm.hello_mm.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
运行 Maven Build,Goals 为 package
项目会被构建到 target 目录下。
运行 java -jar filename.jar 可以看到效果。
文件的路径
在 Maven 项目中,代码的路径一般为 src/main/java,配置文件等的路径为 src/main/resources。
如果代码中涉及到对于配置文件的读取,需通过如下方式。假设配置文件(config/hw.properties)放在 src/main/resources 中。
String path = getClass().getResource("/config/hw.properties").getPath(); // 第一种方式,通过这种方式可以获取文件的路径,后续就不再写了。
InputStream fis = this.getClass().getResourceAsStream("/config/hw.properties"); // 第二种方式,直接获取 InputStream
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
一些错误信息及解决方案
有时会在项目的图标上看到左下角有一个红叉,但是项目中又没有红叉出现,这种情况下并不影响项目的构建,但是如果希望把红叉去掉的话,可以在项目名称上点右键,选择 Maven–Update Project…,红叉消失。
Maven 配置文件第一行位置出现红叉,内容为
org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
解决方案可以参考
m2e error in MavenArchiver.getManifest()
Error in pom.xml, maven issue
Better solution: update Eclipse m2e extensions
From Help > Install New Software.., add a new repository (via the Add.. option), pointing to any of the following URLs:
- https://otto.takari.io/content/sites/m2e.extras/m2eclipse-mavenarchiver/0.17.2/N/LATEST/ or
- http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
Then follow the update wizard as usual. Eclipse would then require a restart. Afterwards, a further Update Project.. on the concerned Maven project would remove any error and your Maven build could then enjoy the benefit of the latest maven-jar-plugin version.
Maven 配置文件路径
Window–Preferences–Maven–User Settings
在配置文件中,可以指定本地 Maven 仓库的路径
<localRepository>E:\repo\maven</localRepository>
用 Maven 构建一个 Java Web Project
How to Create Dynamic Web Project using Maven in Eclipse?
Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application
Struts2+Spring3+Hibernate3+Maven构建(基于Eclipse)
某些包
包所对应的Maven可直接搜索包名+Maven,一般类似这样的链接https://mvnrepository.com/artifact/net.sf.json-lib/json-lib
会很靠前,可参考。以这个链接为例,可以发现,该jar包在2010年后已经不再更新,因此,如果使用 JSON 的话,选取这个 jar 包并非一个明智的选择。或者这个列表中靠前的会更好些。https://mvnrepository.com/tags/json
net.sf.json-lib 需要指明 classifier
可参考Maven 的classifier的作用
Maven repo
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
jnetpcap 需要增加 repository
加入 dependency 后如下Missing artifact jnetpcap:jnetpcap:jar:1.4.r1425-1f
<dependencies>
<dependency>
<groupId>jnetpcap</groupId>
<artifactId>jnetpcap</artifactId>
<version>1.4.r1425-1f</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars</id>
<url>http://clojars.org/repo/</url>
</repository>
</repositories>