Maven generates jar file with main class setting and dependency libs copy

本文介绍如何利用Maven工具简化Java项目的部署过程,包括配置主类、依赖管理和构建jar文件。通过实例演示了如何设置Maven插件来自动化生成jar文件,并配置依赖,避免手动添加外部jar包的问题。最后,还提到了遇到的问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When I write a pure java project, I'm afraid of writing deployment doc. Because I need to generate jar file. The most frustrating thing is add other libs into the jar. Maven is a fabulous tool which gives you different ways to deal with dependencies. In our case, we keep external jars and our jar file separately. We just use maven's plugin, then all things will be done. Let me show you an example:

1. Config main class. In your pom.xml, add a <plugin> inside your <plugins>

<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.1</version>
				<executions>
					<execution>
						<id>default-jar</id>
						<phase>package</phase>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.rujuan.MyMain</mainClass>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<manifestEntries>
							<mode>development</mode>
							<url>${pom.url}</url>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>


2. Config dependencies. Use another plugin, very easy, right?

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<outputDirectory>${project.build.directory}/lib</outputDirectory>
				</configuration>
			</plugin>

If you don't specify outputDirectory, the default one is dependency.

Note: If you use Liferay IDE1.5, when you add maven-dependency-plugin it'll give you an error "maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e." .  It doesn't matter, when you build jar file, choose to use command line.

3. build .jar file

Go to your project folder where your pom.xml is located, run "mvn clean install". It'll generate lib folder within libs and project.jar inside target folder. 

4. run jar file

java -jar project.jar

That's all. That's very easy, right? But I spent almost 1.5 hour on that, util my coworker tells me where to run the jar file. I made a mistake and go to maven repository to run the jar.大笑


BTW, I ran into a problem in my project. I put my properties file inside src/main/java folder, maven ignore it when it builds jar for me. I tried to use <include>, but it excludes other xml files. The reason is I put the properties file in a wrong place. I changed to put it under /scr/main/resources folder, maven automatically include it for me.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值