简体   繁体   English

用依赖Maven构建jar

[英]Building jar with dependencies maven

I can't find a comprehencive guide to how maven building actually works. 我找不到有关Maven构建实际工作原理的全面指南。 And trying to follow the advice I find at stackoverflow I wound up with a monster sized maven build branch that still doesn't work. 并尝试遵循我在stackoverflow上找到的建议,最终遇到了一个仍然无法正常工作的巨型Maven构建分支。 What I wan't is a minimalistic maven build protocol that includes all dependencies and simply just runs. 我所需要的是一个简约的 Maven构建协议,该协议包含所有依赖项,并且只需运行即可。

This is the broken maven file that I have now. 这是我现在拥有的损坏的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>

The easiest thing to build is an executable uper jar which contains all dependencies and can be used via: 最容易构建的是一个可执行的uper jar,它包含所有依赖项,可以通过以下方式使用:

java -jar xxx.jar

This can be archived with the maven-shade-plugin 可以使用maven-shade-plugin存档

So your pom.xml would look like this: 因此,您的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>

So you can build this with maven package and find the uber jar in the target folder. 因此,您可以使用maven package进行构建,并在目标文件夹中找到uber jar。

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

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