简体   繁体   English

在Netbeans中从ant迁移到maven

[英]Migrating from ant to maven in Netbeans

Our software is written in Java and comprise many (7) projects. 我们的软件是用Java编写的,包含许多(7)个项目。

These projects are Netbeans ant projects. 这些项目是Netbeans蚂蚁项目。 I'm considering to converting them to maven2. 我正在考虑将它们转换为maven2。

Where can I find some hints for doing such thing? 我在哪里可以找到做这种事情的一些提示?

Don't read that book. 不要读那本书。 It will only make you confused. 它只会让你感到困惑。 Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ . 请改为阅读本书:“Maven - 权威指南” http://www.sonatype.com/books/maven-book/reference/

Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it. 此外,maven网站有很多信息,但结构很糟糕所以你需要使用谷歌导航它。

Here is my suggestion: 这是我的建议:

  1. Do this by hand, not with "automagic" "help" from the IDE. 手动执行此操作,而不是使用IDE中的“自动”“帮助”。 Maven integration doesn't work that good yet, not in any IDE. Maven集成不能很好地工作,而不是在任何IDE中。

  2. Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. 确保程序项目在一个共同的伞形模块下分成模块,这样每个模块都会生成一个二进制工件(jar,war,...),可能还有该工件后面的源代码的javadoc,一个带有源代码等。基本原则是每个模块产生一个工件,包含该模块下的所有非测试代码。 You can do this while the project is still built by ant. 你可以在项目仍由ant构建时执行此操作。

  3. Each module should conform to the standard maven directory layout. 每个模块都应符合标准的maven目录布局。 The build destination is under [module]/target/[output-type, eg "classes"]. 构建目标位于[module] / target / [output-type,例如“classes”]下。 The source code is under [module]/src/main/[src-type eg "java"] and [module]/test/[src-type]. 源代码在[module] / src / main / [src-type,例如“java”]和[module] / test / [src-type]下。 The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. 该工件包含src / main下的所有代码,以及src / test下的所有代码,因为它构建到目标目录。 You can do this while the is still built by ant. 你可以做到这一点,而仍然是由蚂蚁建造的。

  4. Start by transforming the sub-module that has no dependencies on other modules in the project. 首先转换不依赖于项目中其他模块的子模块。

  5. Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. 现在,您可以使用工件类型“pom”创建父maven模块pom.xml,其中包含以下模块之一。 Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". 使用伞形模块作为“父”,为第一个子模块(仅具有外部依赖性的子模块)创建子模块。 Remember that you need to specify version for the parent. 请记住,您需要为父级指定版本。 Remember to add the child module as a "module" in the parent too. 请记住将子模块添加为父模块中的“模块”。 Always use ${project.version} as version in the child modules when you create multi-module projects like this. 在创建这样的多模块项目时,始终在子模块中使用$ {project.version}作为版本。 All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. 父项下的所有模块必须在一次操作中同时发布,如果使用此设置,maven将确保版本字段在所有模块中保持不变,并在发布期间随处更新。 This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. 这可能使重用现有的编号方案变得困难,但这并不重要。 You are never going to run out of version numbers anyway. 无论如何,你永远不会用完版本号。

  6. Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module. 添加必要的依赖项,并确保可以使用父模块中的命令“mvn clean install”一起构建父模块和子模块。

  7. Proceed with the rest of the modules the same way. 以相同的方式继续使用其余模块。 Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". 对同一父项目下的其他模块的依赖性也应使用$ {project.version}作为它们所依赖的“版本”,意思是“与此版本相同”。 NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. 注意,为了构建,必须使用“mvn install”构建您所依赖的模块,以便将其部署到本地(计算机)存储库。 Otherwise the depending module will not be able to find the classes. 否则,依赖模块将无法找到类。 There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. maven中的模块之间没有源代码依赖关系,只有安装在本地和远程存储库中的构建和打包版本的依赖关系。 This can be very confusing if you come from ant-projects. 如果你来自蚂蚁项目,这可能会非常混乱。 Build from the root module until you get comfortable with this. 从根模块构建,直到您对此感到满意为止。 It takes two days. 这需要两天时间。

  8. Don't use maven integration in IDEs. 不要在IDE中使用maven集成。 It is a bad idea. 这是个坏主意。 Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. 使用“mvn idea:idea”或“mvn eclipse:eclipse”将工作区设置为非maven普通IDE项目。 The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. maven和IDE中的模块间依赖关系机制并不相同,也永远不会。 Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. 此外,如果您有几个具有依赖关系的mavenized项目,您希望在工作区中具有其中的几个,并在它们之间设置依赖关系。 You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." 您可以使用mvn idea:idea / eclipse:eclipse,如果您在与父模块相同的目录中创建一个名为“workspace.xml”(或其他)的单独maven项目文件,则设置为包含模块的多模块项目“ “。 and "../otherproject" (only one-way reference here, no parent ref back). 和“../otherproject”(这里只有单向引用,没有父级引用)。 If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. 如果您运行“mvn idea:idea / eclipse:eclipse -f workspace.xml”,您将获得一个工作空间,其中所有这些模块都链接在一起。 No IDE integration lets you do that. 没有IDE集成可以让你这样做。 This sound like a lot of extra work, but the workspace.xml-file is really small. 这听起来像很多额外的工作,但workspace.xml文件非常小。 It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE. 它不必包含所有依赖项内容,只需要包含对要在IDE中绑定的模块的引用。

I have built a script to migrate Ant builds to Maven. 我已经构建了一个脚本来将Ant构建迁移到Maven。 You can find more information here: 您可以在这里找到更多信息:

https://github.com/ewhauser/ant2maven https://github.com/ewhauser/ant2maven

It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started. 它无法帮助您修复目录结构和任何其他Ant任务,但它消除了许多繁琐的入门步骤。

I discovered that the migration is not necessary. 我发现迁移不是必需的。 The real requirements that I need was automatic download of dependencies (libraries). 我需要的真正要求是自动下载依赖项(库)。

This is also achieved by Ivy which nonetheless uses maven repositories. 这也是由Ivy实现的,尽管如此,它还使用了maven存储库。

I solved converting project from ant to ant+ivy with IvyBeans . 我用IvyBeans解决了将项目从ant转换为ant + ivy的问题

I did a succeful migration of NetBeans Ant project to Maven project using the instruccions by Joseph Mocker here: http://forums.netbeans.org/ptopic55953.html I cite the important part: 我使用Joseph Mocker的指令将NetBeans Ant项目成功迁移到Maven项目: http ://forums.netbeans.org/ptopic55953.html我引用了重要部分:

  1. close the project 关闭项目
  2. rename the build.xml, nbproject files/folders to something so NB won't recognize them. 将build.xml,nbproject文件/文件夹重命名为NB将无法识别它们。
  3. close and restart NB (so any memory cache knowledge of the project is gone) 关闭并重新启动NB(因此项目的任何内存缓存知识都消失了)
  4. copy in an empty pom from some other project. 从其他项目中复制一个空的pom。
  5. open the project back up in NB (NB should now identify it as a maven project) 在NB中打开项目备份(NB现在应该将其标识为maven项目)
  6. rearrange the files to follow the maven way (™) 重新排列文件以遵循maven方式(™)

This won't be an easy task since Maven2 expects the files to be organized in a specific way. 这不是一件容易的事,因为Maven2希望以特定的方式组织文件。 Anyway Better Builds with Maven is a free book that should get you started. 无论如何, 使用Maven构建更好的内容是一本免费的书,可以帮助您入门。 It will help you understand Maven and it also has a chapter on migration. 它将帮助您了解Maven,它还有一章关于迁移。

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

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