简体   繁体   English

使用Maven创建一个独立的应用程序

[英]Create a standalone application with Maven

How do I create a desktop (standalone/Swing) application with Maven? 如何使用Maven创建桌面(独立/ Swing)应用程序?

I'm using Eclipse 3.6. 我正在使用Eclipse 3.6。

  1. Create a Maven project as follows: 创建一个Maven项目如下:

     mvn archetype:generate -DgroupId=com.yourapp.app -DartifactId=swingapp -Dversion=1.0-SNAPSHOT 
  2. Add the following entry to your pom file: 将以下条目添加到您的pom文件中:

     <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <mainClass>com.yourapp.app.YourMainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> 
  3. Import the project into Eclipse as a Maven project, then run as Java application. 将项目作为Maven项目导入Eclipse,然后作为Java应用程序运行。

The following works for me: 以下适用于我:

  1. Create a standard Java project 创建一个标准的Java项目
  2. Create a source folder "src/main/java" 创建源文件夹“src / main / java”
  3. Create a package "testswing" in the source folder 在源文件夹中创建一个包“testswing”
  4. Create a class "App" with a main method 使用main方法创建一个“App”类

     package testswing; import javax.swing.JFrame; public class App { public static void main(String[] args) { JFrame f=new JFrame("Hello World"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } 
  5. Convert to a Maven project (through the Configure... Convert to Maven Project right click menu) 转换为Maven项目(通过Configure ... Convert to Maven Project右键菜单)

  6. Ensure the pom.xml contains a manifest that specifies your main class: 确保pom.xml包含指定主类的清单:

     <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>TestSwing</groupId> <artifactId>TestSwing</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>testswing.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> 
  7. Run a Maven build with the "package" goal (Run As... Maven Build menu) 使用“包”目标运行Maven构建(运行方式... Maven构建菜单)

  8. You should get an executable .jar file that runs as a standalone Swing application 您应该获得一个可执行的.jar文件,该文件作为独立的Swing应用程序运行
archetype used?

A swing application is a standard JAR so just use the standard archetype: swing应用程序是一个标准的JAR,所以只需使用标准的原型:

mvn archetype:generate -DgroupId=com.yourapp.app \
                       -DartifactId=swingapp     \
                       -Dversion=1.0-SNAPSHOT

If you plan to use the standard Swing API only, there aren't no extra dependencies to declare.For extra functionalists you have to use appropriate dependencies in repository 如果您计划仅使用标准Swing API,则不需要声明额外的依赖项。对于额外的功能主义者,您必须在存储库中使用适当的依赖项

UPDATE! UPDATE!

新图标 If you get the following error (Apache Maven 3.3.1): 如果您收到以下错误(Apache Maven 3.3.1):

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.3:create
(default-cli) on project standalone-pom: Unable to parse configuration of mojo org.apache
.maven.plugins:maven-archetype-plugin:2.3:create for parameter #: Cannot create instance 
of interface org.apache.maven.artifact.repository.ArtifactRepository: org.apache.maven.ar
tifact.repository.ArtifactRepository.<init>() -> [Help 1]

Use the following command: 使用以下命令:

mvn archetype:generate -DgroupId=com.test -DartifactId=AppTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

You may want to prefer the command line tool for create the project and you may want to prefer eclipse for development . 您可能希望更喜欢命令行工具来创建项目,您可能希望更喜欢eclipse进行开发

  1. Create the maven project. 创建maven项目。

    Navigate to the eclipse workspace directory and use the next command line: 导航到eclipse工作区目录并使用下一个命令行:

     mvn archetype:create -DgroupId=com.test -DartifactId=AppTest 
  2. Import the project in eclipse: 在eclipse中导入项目:

    In menu File > Import... , select Existing Maven Projects : 在菜单File > Import ...中 ,选择Existing Maven Projects

    导入现有的Maven项目

    Input/ Browse... the eclipse workspace directory (the directory of the previous step) and select the project: 输入/ 浏览... eclipse工作区目录(上一步的目录)并选择项目:

    选择Maven项目

    Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM