eclipse 如何在不破坏 Subversion 历史的情况下重命名 Java 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2485928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:33:32  来源:igfitidea点击:

How to rename Java packages without breaking Subversion history?

javaeclipsesvntortoisesvnrenaming

提问by Joanis

The company I'm working for is starting up and they changed their name in the process. So we still use the package name com.oldname because we are afraid of breaking the file change history, or the ancestry links between versions, or whatever we could break (I don't think I use the right terms, but you get the concept).

我工作的公司正在成立,他们在此过程中更改了名称。所以我们仍然使用包名 com.oldname 因为我们害怕破坏文件更改历史,或者版本之间的祖先链接,或者我们可能破坏的任何东西(我认为我使用的术语不正确,但你明白了这个概念)。

We use: Eclipse, TortoiseSVN, Subversion

我们使用:Eclipse、TortoiseSVN、Subversion

I found somewherethat I should do it in many steps to prevent incoherence between content of .svn folders and package names in java files:

我发现的地方,我应该这样做在很多步骤,以防止在java文件.svn目录和包名的内容之间不连贯:

  • First use TortoiseSVN to rename the directory, updating the .svn directories.
  • Then, manually rename the directory back to the original name.
  • To finally use Eclipse to rename the packages (refactor) back to the new name, updating the java files.
  • 首先使用 TortoiseSVN 重命名目录,更新 .svn 目录。
  • 然后,手动将目录重命名回原始名称。
  • 最后使用 Eclipse 将包(重构)重命名回新名称,更新 java 文件。

That seems good to me, but I need to know if the ancestry and history and everything else will still be coherent and working well.

这对我来说似乎很好,但我需要知道祖先和历史以及其他一切是否仍然一致并且运作良好。

I don't have the keys to that server, that's why I don't hastily backup things and try one or two things. I would like to come up with a good reason not to do it, or a way of doing it which works.

我没有那个服务器的钥匙,这就是为什么我不会匆忙备份东西并尝试一两件事。我想提出一个不这样做的充分理由,或者一种行之有效的方法。

Thank you for your help,

感谢您的帮助,

M. Joanis

M.乔尼斯



Package rename test

包重命名测试

Procedure:

程序:

  1. Create a new package com.oldname.test.renametest.subpackage.
  2. Add a new class under renametest called RenameTest0.java and containing:

    class RenameTest0 {
        public RenameTest0() {
            showMessage();
            new RenameTest1();
        }
        public static void showMessage() {
            System.out.println("RenameTest0!");
        }
        public static void main(String[] args) {
            new RenameTest0();
        }
    }
  3. Add a new class under renametest.subpackage containing:

    class RenameTest1 {
        public RenameTest1() {
            showMessage();
            RenameTest0.showMessage();
        }
        public static void showMessage() {
            System.out.println("RenameTest1!");
        }
    }
  4. Test that RenameTest0 runs fine.

  5. Commit.
  6. Change the messages of both of the classes.
  7. Commit.
  8. Again, change the message of one class and commit (just creating some history).
  9. Apply procedure proposed above (the three steps in the original message) for renaming package renametest to testrename.
  10. Commit.
  11. Test run.
  12. Modify the messages again and test.
  13. Commit.
  14. Try to roll back to the version when both messages have been changed simultaneously the first time.
  15. If everything worked fine to this point, it looks good, no?
  1. 创建一个新包 com.oldname.test.renametest.subpackage。
  2. 在 renametest 下添加一个名为 RenameTest0.java 的新类,其中包含:

    class RenameTest0 {
        public RenameTest0() {
            showMessage();
            new RenameTest1();
        }
        public static void showMessage() {
            System.out.println("RenameTest0!");
        }
        public static void main(String[] args) {
            new RenameTest0();
        }
    }
  3. 在 renametest.subpackage 下添加一个新类,其中包含:

    class RenameTest1 {
        public RenameTest1() {
            showMessage();
            RenameTest0.showMessage();
        }
        public static void showMessage() {
            System.out.println("RenameTest1!");
        }
    }
  4. 测试 RenameTest0 是否运行良好。

  5. 犯罪。
  6. 更改两个类的消息。
  7. 犯罪。
  8. 再次,更改一个类的消息并提交(只是创建一些历史记录)。
  9. 应用上面提出的程序(原始消息中的三个步骤)将包 renametest 重命名为 testrename。
  10. 犯罪。
  11. 测试运行。
  12. 再次修改消息并测试。
  13. 犯罪。
  14. 尝试回滚到第一次同时更改两条消息时的版本。
  15. 如果到目前为止一切正常,它看起来不错,不是吗?

Result of test:

测试结果:

  • Note on step 9: Had to do it in reverse order(Eclipse rename THEN TortoiseSVN rename.), else it was getting complicated, as TSVN create a new folder/package and marks the old one for deletion... So you can't rename for Eclipse unless you put the old package somewhere else in the meantime to prevent losing .svn folders, etc. etc. Didn't look like a good idea to go further with this method. (Note to myself: don't forget to tick the checkbox for recursive package renaming!)
  • Note on step 14: Worked! We can see previous versions; all we have to do is tell not to break on copy/move and it's ok. Once reverted to a version before the rename, the package names are not back to the good name though, probably that refactoring it again would do it.
  • End note: I was surprised to have to do the critical steps in reverse order. To do that right in the middle of this first package rename try, I had to roll back some TSVN and manual modifications, casting a little doubt on the repeatable nature of the exact results of this procedure. I will have to do a second test to confirm it's validity. To sum up: it looks good, but needs further testing.
  • 关于第 9 步的注意事项:必须以相反的顺序进行(Eclipse 重命名 THEN TortoiseSVN 重命名。),否则它会变得复杂,因为 TSVN 创建一个新文件夹/包并将旧文件夹/包标记为删除......所以你不能为 Eclipse 重命名,除非您同时将旧包放在其他地方以防止丢失 .svn 文件夹等。使用此方法更进一步看起来不是一个好主意。(请注意:不要忘记勾选递归包重命名的复选框!)
  • 第 14 步的注意事项:成功了!我们可以看到以前的版本;我们所要做的就是告诉不要在复制/移动时中断,就可以了。一旦恢复到重命名之前的版本,包名称就不会恢复为好名称,可能再次重构它会做到这一点。
  • 尾注:我很惊讶必须以相反的顺序执行关键步骤。为了在第一次包重命名尝试中做到这一点,我不得不回滚一些 TSVN 和手动修改,对这个过程的确切结果的可重复性产生了一点怀疑。我将不得不进行第二次测试以确认其有效性。总结:看起来不错,但需要进一步测试。

采纳答案by álvaro González

Perhaps it's not practical for your exact needs but TortoiseSVN has a handy feature regarding renames. You could do this:

也许这对于您的确切需求不切实际,但 TortoiseSVN 有一个关于重命名的方便功能。你可以这样做:

  1. Use your IDE's refactoring feature to rename stuff.
  2. Launch the "Check for modifications" dialogue from TortoiseSVN.
  3. For each renamed item, you'll see two entries: a missing "source.java" item and an unversioned "target.java" item. Highlight both and choose "Repair move" from the context menu.
  1. 使用 IDE 的重构功能来重命名内容。
  2. 从 TortoiseSVN 启动“检查修改”对话框。
  3. 对于每个重命名的项目,您将看到两个条目:一个缺失的“source.java”项目和一个未版本化的“target.java”项目。突出显示两者并从上下文菜单中选择“修复移动”。

Repair moves/renames

修复移动/重命名

回答by markusk

Have you considered using the Subclipse plugin? It may solve your problems, according to How do I use Eclipse Refactoring Tools and stay in sync with SVN through Subclipse?

您是否考虑过使用Subclipse 插件?它可能会解决您的问题,根据如何使用 Eclipse 重构工具并通过 Subclipse 与 SVN 保持同步?

回答by Karussell

Are you sure keeping history is NOT working if you are using the refactoring method included in eclipse?

如果您使用 eclipse 中包含的重构方法,您确定保留历史记录不起作用吗?

With NetNeans I regularly change package names and the underlying 'svn plugin' will silently movethe content (which saves history) into the new directory (after that the normal refactoring will happen).

使用 NetNeans,我会定期更改包名称,底层的“svn 插件”会默默地内容(保存历史记录)移动到新目录中(之后将进行正常的重构)。

so: Have you tried it from within eclipse if the history is kept with the subversion plugin? (e.g. in a fresh check-out copy to avoid failure)

所以:如果历史记录与 subversion 插件一起保存,您是否在 eclipse 中尝试过?(例如在新的结帐副本中以避免失败)

At least you could use NetBeans to do this one-time task ...

至少您可以使用 NetBeans 来完成这项一次性任务......

回答by jayshao

You cando this, and it's not that hard - your best bet to get a clean SVN history is to do it in 2 steps (can become one commit) - though for good results I recommend using the CLI client.

可以做到这一点,而且并不难——获得干净的 SVN 历史记录的最佳选择是分两步完成(可以成为一次提交)——尽管为了获得良好的结果,我建议使用 CLI 客户端。

  1. Use svn mv to move the folders/packages
  2. Go into Eclipse, or use grep from the CLI to fix the packages in the files to match the new name
  1. 使用 svn mv 移动文件夹/包
  2. 进入 Eclipse,或使用 CLI 中的 grep 修复文件中的包以匹配新名称

Then you can commit as a change-set, and the history at the file level should match.

然后您可以作为更改集提交,并且文件级别的历史记录应该匹配。

If you're using Maven or a packaging tool, recommend you run a release before doing something like this - also it's worth cutting a tag immediately before this in case you need to go back to the old structure

如果您正在使用 Maven 或打包工具,建议您在执行此类操作之前运行一个版本 - 在此之前立即切割标签也是值得的,以防您需要返回旧结构

回答by Don Kirkby

Yes, it will work. You could install the command line version of svn and write a batch file that will do the svn stuff. Automating the eclipse stuff would be a bit more work, and probably not worth it unless you're already familiar with the eclipse API.

是的,它会起作用。您可以安装 svn 的命令行版本并编写一个批处理文件来执行 svn 的工作。自动化 eclipse 的东西需要更多的工作,除非您已经熟悉 eclipse API,否则可能不值得。

Test it with one package before you do everything just to make sure you're doing all the steps right.

在你做任何事情之前用一个包测试它,以确保你做的所有步骤都是正确的。

回答by gogognome

I discovered that the subclipse plugin gives the error message " is already under version control" when committing a class that has been moved to a new package (i.e. not under source control yet) and the parent of this package is also new.

我发现 subclipse 插件在提交已移动到新包(即尚未受源代码控制)的类时给出错误消息“已在版本控制下”,并且该包的父级也是 new

When this happens, I can commit the changes using TortoiseSVN. After that I only need to refresh the project in Eclipse.

发生这种情况时,我可以使用 TortoiseSVN 提交更改。之后我只需要在 Eclipse 中刷新项目。

After moving a class to a new package whose parent is already under source control, subclipse can commit this change without problems.

将一个类移动到一个新的包,它的父类已经在源代码控制之下,subclipse 可以毫无问题地提交这个更改。

回答by jule64

Instead of renaming the packages you could do this:

您可以这样做,而不是重命名包:

  1. create the new package structure in your project. Once done your project should look something like this:

           com -
               |- myOLDcompname -
               |                |- feature1 -
               |                            |- classA.java
               |                            |- classB.java
               |- myNEWcompname -
                                |- feature1
    
  2. add the new folders under version control so svn can track them

  3. move your java classes from old packages to new ones. Eclipse should update all the classes imports and package declarations accordingly. Most importantly because old and new packages are under vcs this step should keep the history of the classes.
  4. when done delete the old folders
  5. commit!
  1. 在您的项目中创建新的包结构。完成后,您的项目应如下所示:

           com -
               |- myOLDcompname -
               |                |- feature1 -
               |                            |- classA.java
               |                            |- classB.java
               |- myNEWcompname -
                                |- feature1
    
  2. 在版本控制下添加新文件夹,以便 svn 可以跟踪它们

  3. 将您的 java 类从旧包移动到新包。Eclipse 应该相应地更新所有类导入和包声明。最重要的是,因为新旧包都在 vcs 下,这一步应该保留类的历史记录。
  4. 完成后删除旧文件夹
  5. 犯罪!