简体   繁体   English

如何在Maven中创建可执行文件?

[英]How to create an executable in maven?

I am working on a compiler project. 我正在一个编译器项目上。 What I am suppose to do is, I have to generate an executable called "ctop" using maven. 我想做的是,我必须使用maven生成一个名为“ ctop”的可执行文件。 By using that executable I have to be able to do "ctop cfile pfile" which will generate an assembly file called "pfile" from a program in c called "cfile". 通过使用该可执行文件,我必须能够执行“ ctop cfile pfile”,它将从c中名为“ cfile”的程序生成一个名为“ pfile”的汇编文件。 As I have never used maven before I have no idea how to do that. 由于我从未使用过maven,所以我不知道该怎么做。 I'm using eclipse IDE and I have created maven project as well. 我正在使用eclipse IDE,也创建了maven项目。 But I have no idea how to work with maven. 但是我不知道如何使用Maven。 I have a main file located at (/src/main/java/me/name/project_compiler/Main.java). 我有一个主文件位于(/src/main/java/me/name/project_compiler/Main.java)。 I have to be able to execute this and other mains. 我必须能够执行此主程序和其他主程序。

My pom.xml file looks like this 我的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>me.name</groupId>
  <artifactId>project-compiler</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>project-compiler</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
</project>

Thanks in advance! 提前致谢!

The best teacher to this question is the following article, assume that you have basic maven knowledge: Maven-Assembly-Plugin 最好的老师是下面的文章,假定您具有基本的Maven知识: Maven-程序集-插件

Next is a simple example: 接下来是一个简单的示例:

1. configure the assembly plugin to ur pom.xml, that means add the assembly plugin config to the pom's build/plugins module. 1.将程序集插件配置为ur pom.xml,这意味着将程序集插件配置添加到pom的build / plugins模块。 for u, add the below code after dependencies, don't forget to change the mainClass. 对于u,请在依赖项之后添加以下代码,不要忘记更改mainClass。

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>     <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                    <archive>
                        <manifest>
                            <mainClass>urpackage.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2. edit the assembly.xml file, the field such as project.build.directory is maven self-supported 2.编辑assembly.xml文件,诸如project.build.directory之类的字段是maven自支持的

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
        <id>bin</id>
        <formats>
            <!--u have many other choices-->
            <format>tar.bz2</format>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <!--put fileSet config to include or exclude files in ur final package-->
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory>${project.artifactId}</outputDirectory>
                <includes>
                    <include>${project.build.finalName}.${project.packaging}</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}/lib</directory>
                <outputDirectory>${project.artifactId}/lib</outputDirectory>
                <includes>
                    <include>*</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>

3. run: mvn clean package -Dmaven.test.skip=true, then you will get what you want in your target directory, just unpackage&check it 3.运行:mvn clean package -Dmaven.test.skip = true,然后将在目标目录中获得所需的内容,只需解压缩并检查它

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

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