简体   繁体   English

如何在Eclipse中的Maven依赖项中编辑`.class`文件

[英]how to edit `.class` file in Maven dependency in Eclipse

Ok, so I have my Java projects, and under the Maven dependency project folder on the project explorer, I encounter a jar folder, and in it there is a .class file.好的,所以我有我的 Java 项目,在项目资源管理器的 Maven 依赖项目文件夹下,我遇到了一个jar文件夹,其中有一个.class文件。 Now, I clicked on Download Sources and I am able to view the .class file as a Java file, but I cannot edit it in Eclipse.现在,我单击“ Download Sources ,可以将.class文件视为 Java 文件,但无法在 Eclipse 中对其进行编辑。

What should I do?我应该怎么办?

You cannot edit .class file(s).您不能编辑.class文件。 You can only edit java file(s) contained in the source code and recompile.您只能编辑包含在源代码中的java文件并重新编译。

The sources that are downloaded by maven are a project SNAPSHOT generated by the maven source plugin for a given project at a given time, to aid in investigating the code, but certainly not to modify it. maven下载的maven source pluginmaven source plugin在给定时间为给定项目生成的一个项目SNAPSHOT,用于辅助调查代码,但绝对不能修改。

These sources can be bound to the classpath and used for navigating the project source in an IDE, but they still remain read-only.这些源可以绑定到类路径并用于在 IDE 中导航项目源,但它们仍保持只读状态。

You cannot edit jars and source jars directly.您不能直接编辑 jar 和源 jar。 You'd need to download the source code, and edit that.您需要下载源代码并对其进行编辑。 You'd then have to go through the build process to re-build the jar, and install it as a new version in your maven repository.然后,您必须完成构建过程以重新构建 jar,并将其作为新版本安装在您的 Maven 存储库中。

Class files are compiled binaries poised to be part of executable programs.类文件是经过编译的二进制文件,准备成为可执行程序的一部分。

To modify them is an art in modifying compiled source code.修改它们是修改编译源代码的艺术。 You probably don't want to do that.你可能不想这样做。 If you want to generate a replacement, download the whole project and recompile it.如果要生成替换,请下载整个项目并重新编译。

You may be obligated to do certain other things under such an action, by law you must abide by the licensing of the software, and recompiling and redistributing (which is what you'll be doing if you must recompile) often puts much heavier burdens on the developer than just "using" (depending on the licensing).在这种行为下您可能有义务做某些其他事情,根据法律,您必须遵守软件的许可,并且重新编译和重新分发(如果您必须重新编译,这就是您将要做的事情)通常会给您带来更重的负担开发人员不仅仅是“使用”(取决于许可)。

As has been previously mentioned, you can't modify .class files.如前所述,您不能修改 .class 文件。 You can download the source java files of the dependencies if available via:如果可用,您可以通过以下方式下载依赖项的源 java 文件:

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

(for confused readers: it's about Eclipse knowing via Maven config how to jump to the known/available (possibly own) project source file to edit it when it is actually linked in the code via some previously packaged jar (with *.class files in it)) (对于困惑的读者:这是关于 Eclipse 通过 Maven 配置知道如何跳转到已知/可用(可能是自己的)项目源文件来编辑它,当它通过一些以前打包的 jar(带有*.class文件)在代码中实际链接时它))

(We had the same problem, but it had worked before after some adjustments and we spend quite some time to figure this out ... ) (我们遇到了同样的问题,但经过一些调整后它已经工作了,我们花了很长时间来解决这个问题......)

our (typical EAR archetype) dir structure:我们的(典型的 EAR 原型)目录结构:

[projx]
  |- [ejb]
       |- pom.xml
            ...
            <!-- A) PROBLEM: dependency resolution can't work in [web]/src/**/* -->
            <version>1.0-SNAPSHOT</version>

            <!-- B) OK: this is what we (and probably you) wanted -->
            <!-- <version>2.0.1-SNAPSHOT</version> -->
            ...
  |- [web]
       |- pom.xml
            ...
            <version>2.0.1-SNAPSHOT</version>
            ...
            <dependency>
                <groupId>...</groupId>
                <artifactId>projx-ejb</artifactId>
                <type>ejb</type>

                <!-- C) a specific older version here would help it as well 
                        if you need it -->
                <!-- <version>1.0.3.Final</version> -->

                <scope>provided</scope>
            </dependency>
            ...
  |- [ear]
       |- pom.xml
            ...
            <version>2.0.1-SNAPSHOT</version>
            ...
  |- pom.xml

       <project ...>
        ...
        <version>2.0.1-SNAPSHOT</version>
        ...
        <modules>
          <module>ejb</module>
          <module>web</module>
          <module>ear</module>
        </modules>
        ...

So the problem was that we forgot to increase the [ejb]/pom.xml//project/version (A)) .所以问题是我们忘记增加[ejb]/pom.xml//project/version (A)) After adjusting it similar to B) (we actually reference a global [projx]/pom.xml//project/properties/projx.version property), it worked again like a charm.调整后类似于 B) (我们实际上引用了一个全局[projx]/pom.xml//project/properties/projx.version属性),它再次像魅力一样工作

(If we would have had depended explicitely on an older, eg `projx-ejb-1.0.3.Final.jar, JAR version, we could have gone for the C) approach where of course the workspace resolution again would not work, rightously.) (如果我们已经明确地依赖于较旧的,例如`projx-ejb-1.0.3.Final.jar,JAR 版本,我们可以选择 C)方法,当然工作区解析再次不起作用,这是理所当然的.)

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

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