如何用IDE组织java项目?

teg*_*goo 4 java project-organization intellij-idea

我要开发java系统,它包含三个应用程序.此应用使用相同的包.如何组织这个项目,例如,IntelliJ IDEA?必须在项目的所有源中组织一个包的层次结构或在库上使用的不同项目.你能告诉我专业解决方案吗?

Ste*_*lly 10

专业的解决方案不是依靠IDE来构建您的软件.您想使用构建工具来构建软件.有许多构建工具可供选择,例如

  1. 使
  2. 蚂蚁
  3. Maven的
  4. 摇篮
  5. Buildr

等等

您选择的工具取决于您如何看待构建软件.

例如,Make,ANT等工具采用"完全按照我说的方式"构建软件的方法.如果您想要明确说明您想要完成的工作以及您希望如何完成工作,这可能会很好.

像Maven这样的工具采用"约定优于配置"的方法.他们说如果遵循这些约定,你就不必反复告诉构建工具如何做标准事情,所以例如使用Maven,如果你将Java源代码放在src/main/java目录中,那么Maven会自动为你编译它,如果您将测试源代码放入其中,src/test/java那么Maven将为您编译并运行所有测试.

Maven并不适合每个人...有些人似乎想花更多的时间告诉构建工具到底要做什么,而不是写软件的时间......这很好......不是我(我在Maven上) PMC ......猜测我最喜欢的构建工具是没什么惊喜的......)但是这些人确实喜欢Maven给出的一些"约定优于配置"的东西......他们只是想在需要时更灵活地覆盖它们离开会议...... Gradle和Buildr等工具被那些具有这种心态的人使用.

好的IDE将获取构建文件并从该构建工具推断项目结构.IntelliJ,Eclipse和NetBeans都了解Maven的pom.xml构建文件.它使您的IDE设置为无操作.我没有调查其他构建工具如何在IDE中公平,因为我个人使用Maven和IntelliJ.

选择一个构建工具,看看它是如何工作的.我假设您正在使用版本控制系统...如果您正在更改构建工具应该不是什么大问题.

如果您选择使用Maven,您将从一个4或5模块项目开始(一个父模块和三个子模块,可能有一个共享的公共模块)

代码的结构如此

+- pom.xml (the root parent pom)
+- common (the common module directory)
|    +- pom.xml (the common module pom)
|    \- src
|         +- main
|         |    +- java
|         |    |    \- com... (your package dirs for common classes)
|         |    \- resources
|         |         \- com... (your package dirs for common classpath resources)
|         \- test
|              +- java
|              |    \- com... (your package dirs for tests of common classes)
|              \- resources
|                   \- com... (your package dirs for common test classpath resources)
+- app1 (the app1 module directory)
|    +- pom.xml (the app1 module pom)
|    \- src
|         +- main
|         |    +- java
|         |    |    \- com...
|         |    \- resources
|         |         \- com...
|         \- test
|              +- java
|              |    \- com...
|              \- resources
|                   \- com...
+- app2 (the app2 module directory)
|    +- pom.xml (the app2 module pom)
|    \- src
|         +- main
|         |    +- java
|         |    |    \- com...
|         |    \- resources
|         |         \- com...
|         \- test
|              +- java
|              |    \- com...
|              \- resources
|                   \- com...
\- app3 (the app3 module directory)
     +- pom.xml (the app3 module pom)
     \- src
          +- main
          |    +- java
          |    |    \- com...
          |    \- resources
          |         \- com...
          \- test
               +- java
               |    \- com...
               \- resources
                    \- com...
Run Code Online (Sandbox Code Playgroud)

你的根pom.xml看起来像

<project>    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mydomain.something</groupId>
    <artifactId>something-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>common</module>
        <module>app1</module>
        <module>app2</module>
        <module>app3</module>
    </modules>
</project>
Run Code Online (Sandbox Code Playgroud)

common/pom.xml看起来像

<project>    
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mydomain.something</groupId>
        <artifactId>something-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>something-common</artifactId>
    <dependencies>
        <!-- insert the 3rd party dependencies you want -->
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

然后每个app poms看起来都像这样

<project>    
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mydomain.something</groupId>
        <artifactId>something-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>something-app1</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.mydomain.something</groupId>
            <artifactId>something-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- insert any 3rd party dependencies you want for app1 only -->
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助