简体   繁体   English

创建带有源代码的可执行jar进行调试

[英]create an executable jar with source code for debug

I know how to create a JAR file with the source code with maven, and also create an executable JAR. 我知道如何使用maven使用源代码创建JAR文件,以及如何创建可执行JAR。 But my question is if there is any way of creating an executable JAR (so it contains all the .class files) that also contains the source code (so for each .class its corresponding .java file). 但是我的问题是,是否有任何方法来创建可执行的JAR(因此它包含所有.class文件),而该JAR也包含源代码(因此,对于每个.class其相应的.java文件)。

I want this for debugging with eclipse since my maven project is a library for another project and when debugging the bigger project I loose track when the execution reaches my library because the source code is missing in the JAR. 我想用eclipse进行调试,因为我的maven项目是另一个项目的库,并且在调试较大的项目时,由于JAR中缺少源代码,我无法跟踪执行到我的库的时间。 I've found a solution among this forums that suggests to create two jars, one executable and another with .java files and set the source path in the IDE to point to the code JAR, but I would like to work with a unique JAR instead of two. 我在这个论坛中找到了一个解决方案,建议创建两个jar,一个可执行文件,另一个使用.java文件,并在IDE中将源路径设置为指向代码JAR,但我想使用唯一的JAR代替两个。 Is that possible? 那可能吗? if not with maven with another java tool? 如果不与另一个Java工具与Maven的? For instance merging both JAR files? 例如合并两个JAR文件?

Lots of thanks. 非常感谢。

You can declare your source directory as additinal resource directory 您可以将源目录声明为附加资源目录

<build>
    <resources>
        <resource>
            <directory>${project.build.sourceDirectory}</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

This will place the java files next to the class files - I do not know if your IDE supports debugging with this structure. 这会将Java文件放在类文件旁边-我不知道您的IDE是否支持使用此结构进行调试。 Usually @Brian Agnew suggested way is the way to go. 通常@Brian Agnew建议的方法是要走的路。

I would have thought that if you're usign Maven you could create the binary as one deployable and the source jar as another. 我本以为如果您使用Maven,则可以将二进制文件创建为一个可部署文件,将源jar创建为另一个。 That way, your IDE should be able to transparently load the source corresponding to the binary jar you're using (in IDE it gives me a 'download' option). 这样,您的IDE应该能够透明地加载与您正在使用的二进制jar相对应的源(在IDE中,它为我提供了“下载”选项)。

See this link for more info. 有关更多信息,请参见此链接

The maven-source-plugin can be used to generate a source code jarW file for a project. maven-source-plugin可用于生成项目的源代码jarW文件。 Deploying this source jar to a remote repository can be useful for other developers so that they can 'attach source' and debug into the project source code. 将此源jar部署到远程存储库对于其他开发人员可能非常有用,这样他们就可以“附加源代码”并调试到项目源代码中。

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

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