且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

只在Maven中创建可执行的jar-with-dependencies

更新时间:2022-01-17 02:29:10

我想我已经弄清楚如何做到这一点。你必须离开jar,否则你将失去编译器和资源复制,以及建立你需要的jar所带来的一切。

I think I've figured out how to do this. You have to leave the jar, otherwise you'll lose the compiler and resource copying, and everything else that goes along with building a jar that you need.

我的解决方案涉及基本上是欺骗maven,所以我想要提醒的是,在未来的版本中这很可能无法正常工作......

My solution involves essentially tricking maven, so I guess the thing to beware of is that it's very possible that in future releases this won't work...

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>dumpster-diver</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>DumpsterDiver</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>

请注意,在列出的构建配置中,maven-assembly-plugin和build的finalName本身就配。另请注意< appendAssemblyId> false< / appendAssemblyId> 。如果这样做,构建和程序集jar的输出文件名将完全匹配。 Maven肯定会警告你,但是会把你想要的jar放在你的目标目录和你的本地存储库中。

Note that in the build configuration listed, the finalName of both the maven-assembly-plugin AND the build itself match. Also note the <appendAssemblyId>false</appendAssemblyId>. If you do this, the output filename of the build and of the assembly jar will match exactly. Maven will definitely warn you about this, but will put the jar you want in your target directory AND in your local repository.

这是'mvn clean install'的输出在这个项目上,以及目标目录和本地存储库中的jar上的jar -tvf:

Here is the output of 'mvn clean install' on this project, as well as jar -tvf on both the jar in the target directory and in the local repository:

$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building dumpster-diver
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory <currentDirectory>/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to <currentDirectory>/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory <currentDirectory>/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[INFO] [assembly:single {execution: default}]
[INFO] Processing DependencySet (output=)
[WARNING] Artifact: <groupId>:dumpster-diver:jar:0.1.0-SNAPSHOT references the same file as the assembly destination file. Moving it to a temporary location for inclusion.
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: <currentDirectory>/target/dumpster-diver.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: <currentDirectory>/target/archive-tmp/dumpster-diver.jar
with assembly file: <currentDirectory>/target/dumpster-diver.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing <currentDirectory>/target/dumpster-diver.jar to /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Aug 18 13:08:56 EDT 2011
[INFO] Final Memory: 22M/618M
[INFO] ------------------------------------------------------------------------
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf ./target/dumpster-diver.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
       126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
   126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties