maven 打包独立可运行jar

本文详细介绍了如何使用Eclipse IDE和Maven插件将Java代码打包成部署包,包括创建和构建项目、添加依赖、构建jar文件等步骤。

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

Creating a .jar Deployment Package Using Maven and Eclipse IDE (Java)

This section shows how to package your Java code into a deployment package using Eclipse IDE and Maven plugin for Eclipse.

Topics

Before You Begin

Install the Maven Plugin for Eclipse.

  1. Start Eclipse. From the Help menu in Eclipse, choose Install New Software.

  2. In the Install window, type http://download.eclipse.org/technology/m2e/releases in the Work with: box, and choose Add.

  3. Follow the steps to complete the setup.

Step 1: Create and Build a Project

In this step, you start Eclipse and create a Maven project. You will add the necessary dependencies, and build the project. The build will produce a .jar, which is your deployment package.

  1. Create a new Maven project in Eclipse.

  • Group Id: doc-examples

  • Artifact Id: lambda-java-example

  • Version: 0.0.1-SNAPSHOT

  • Packaging: jar

  • Name: lambda-java-example

  1. From the File menu, choose New, and then choose Project.

  2. In the New Project window, choose Maven Project.

  3. In the New Maven Project window, choose Create a simple project, and leave other default selections.

  4. In the New Maven ProjectConfigure project windows, type the following Artifact information:

Add the aws-lambda-java-core dependency to the pom.xml file.

It provides definitions of the RequestHandlerRequestStreamHandler, and Context interfaces. This allows you to compile code that you can use with AWS Lambda.

  1. Open the context (right-click) menu for the pom.xml file, choose Maven, and then choose Add Dependency.

  2. In the Add Dependency windows, type the following values:

    Group Id: com.amazonaws

    Artifact Id: aws-lambda-java-core

    Version: 1.1.0

    Caution

    If you are following other tutorial topics in this guide, the specific tutorials might require you to add more dependencies. Make sure to add those dependencies as required.

Add Java class to the project.

  • Packageexample

  • NameHello

    Caution

    If you are following other tutorial topics in this guide, the specific tutorials might recommend different package name or class name.

  1. Open the context (right-click) menu for the src/main/java subdirectory in the project, choose New, and then choose Class.

  2. In the New Java Class window, type the following values:

  3. Add your Java code. If you are following other tutorial topics in this guide, add the provided code.

Build the project.

Open the context (right-click) menu for the project in Package Explorer, choose Run As, and then chooseMaven Build .... In the Edit Configuration window, type package in the Goals box.

Note

The resulting .jar, lambda-java-example-0.0.1-SNAPSHOT.jar, is not the final standalone .jar that you can use as your deployment package. In the next step, you add the Apache maven-shade-plugin to create the standalone .jar. For more information, go to Apache Maven Shade Plugin.

Add the maven-shade-plugin plugin and rebuild.

The maven-shade-plugin will take artifacts (jars) produced by the package goal (produces customer code .jar), and created a standalone .jar that contains the compiled customer code, and the resolved dependencies from the pom.xml.

  • Group Id: org.apache.maven.plugins

  • Artifact Id: maven-shade-plugin

  • Version: 2.3

  1. Open the context (right-click) menu for the project, choose Run As, and then choose Maven build ....

  2. In the Edit Configuration windows, type package shade:shade in the Goals box.

  3. Choose Run.

    You can find the resulting standalone .jar (that is, your deployment package), in the /targetsubdirectory.

    Open the context (right-click) menu for the /target subdirectory, choose Show In, choose System Explorer, and you will find the lambda-java-example-0.0.1-SNAPSHOT.jar.

  4. Open the context (right-click) menu for the pom.xml file, choose Maven, and then choose Add Plugin.

  5. In the Add Plugin window, type the following values:

  6. Now build again.

    This time we will create the jar as before, and then use the maven-shade-plugin to pull in dependencies to make the standalone .jar.


转载于:https://my.oschina.net/u/2308739/blog/614680

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值