繁体   English   中英

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

[英]How to create an executable in maven?

我正在一个编译器项目上。 我想做的是,我必须使用maven生成一个名为“ ctop”的可执行文件。 通过使用该可执行文件,我必须能够执行“ ctop cfile pfile”,它将从c中名为“ cfile”的程序生成一个名为“ pfile”的汇编文件。 由于我从未使用过maven,所以我不知道该怎么做。 我正在使用eclipse IDE,也创建了maven项目。 但是我不知道如何使用Maven。 我有一个主文件位于(/src/main/java/me/name/project_compiler/Main.java)。 我必须能够执行此主程序和其他主程序。

我的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>

提前致谢!

最好的老师是下面的文章,假定您具有基本的Maven知识: Maven-程序集-插件

接下来是一个简单的示例:

1.将程序集插件配置为ur pom.xml,这意味着将程序集插件配置添加到pom的build / plugins模块。 对于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.编辑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.运行:mvn clean package -Dmaven.test.skip = true,然后将在目标目录中获得所需的内容,只需解压缩并检查它

暂无
暂无

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

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