java - 使用依赖项 maven 构建 jar

标签 java maven jar build

我找不到关于 Maven 构建实际工作原理的全面指南。并试图遵循我在 stackoverflow 上找到的建议,结果我得到了一个巨大的 Maven 构建分支,但仍然无法工作。我想要的是一个简约 Maven 构建协议(protocol),其中包含所有依赖项并且只是运行。

这是我现在拥有的损坏的 Maven 文件。

<?xml version="1.0" encoding="UTF-8"?>
<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>image-converter</groupId>
    <artifactId>image-converter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>image-converter-${project.version}</finalName>
        <directory>../</directory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${project.build.directory}/lib</classpathPrefix>
                            <mainClass>com.luttikDevelopment.imageConverter.Converter</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最佳答案

最容易构建的是一个可执行的 super jar,它包含所有依赖项,可以通过以下方式使用:

java -jar xxx.jar

可以使用 maven-shade-plugin 进行存档

所以你的 pom.xml 看起来像这样:

 <?xml version="1.0" encoding="UTF-8"?>
<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>image-converter</groupId>
<artifactId>image-converter</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20141113</version>
    </dependency>
</dependencies>

<build>
    <finalName>image-converter-${project.version}</finalName>
    <plugins>                       
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                    
            <mainClass>com.luttikDevelopment.imageConverter.Converter</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
    </plugins>
</build>

因此,您可以使用 maven package 构建它,并在目标文件夹中找到 uber jar。

关于java - 使用依赖项 maven 构建 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034698/

相关文章:

java - 为什么大多数网络托管服务仅支持 PHP 作为服务器端语言?

javascript - 功能完成时加载按钮

java - Maven "package"与 Eclipse 中导出为 war

python - 使用 xPath 从 pom 中提取版本号时遇到问题

spring - mvn Spring 启动 :run vs Run

java - 在 Android 中使用 httpUrlConnection 发送授权 header

java - 是什么导致了 java.lang.NoSuchMethodError : com. fastxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

java - 为什么gradle创建的jar文件中没有.class文件?

java - 从批处理文件中运行具有依赖项的 jar

java - 修改 jar 中的文件