在Maven中创建2个Jars(可移植和依赖项)时的最佳实践。

huangapple go评论57阅读模式
英文:

Best practices when creating 2 Jars (Portable and Dependency) in Maven

问题

我创建了一个小工具来签署PDF文件。现在我想要有两个JAR文件,一个是便携式的JAR文件,只需执行该JAR文件即可,另一个JAR文件可以加载到其他项目中并使用。

我的当前方法:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>BatchPDFSign</finalName>
                    <archive>
                        <manifest>
                            <mainClass>BatchPDFSign.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </build>

正如你所看到的,我的第一种方法是使用maven-assembly-plugin创建一个Uber Jar,并使用maven-compiler-plugin创建普通的Jar。

但我觉得我可能做错了,有更好的方法可以做到这一点。我在Maven中还比较新手。

简而言之:有没有一种最佳方法可以一次性创建一个包含所有依赖项的Jar和一个适用于上传到Maven的Jar?

英文:

I created a small tool to sign PDF's. Now I would like to have 2 Jars, one Portable Jar where you can just execute the Jar and thats it and a Jar which can be loaded into other projects and used.

My current approach:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>BatchPDFSign</finalName>
                    <archive>
                        <manifest>
                            <mainClass>BatchPDFSign.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </build>

As you can see my first approach was to create an Uber Jar with the maven-assembly-plugin and the normal Jar with the maven-compiler-plugin.

But I don't loose the feeling that I've done it totaly wrong and there is a much better way to do this. I am quite new with maven.

tl;dr: what is the best way to create a Jar with all the dependencies and a Jar suitable to be uploaded to maven in one swoop?

答案1

得分: 2

通常情况下,您会创建一个具有两个模块的多模块项目:

  • 库模块。
  • 可执行的JAR 模块。

可执行的JAR 模块将库模块作为依赖项,并添加运行所需的内容(可能包括一个主类和一些配置)。

然后,其他人也可以使用该库模块。

英文:

Usually, you would create a multi-module project with two modules:

  • The library.
  • The executable jar.

The executable jar uses the library as dependency and adds what is needed to run it (probably a Main class and some configuration).

Then the library can also be used by anybody else.

huangapple
  • 本文由 发表于 2020年7月22日 15:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63029331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定