Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize java project with IDE?

I'm going to develop java system, that consists of three apps. This apps use same packages. How to organize this project in, for example, IntelliJ IDEA? Must it be on project all sources organised in one hierarchy of packages or different projects which use on library. Could you tell me professional solution?

like image 231
Vladimir Berlev Avatar asked Jan 15 '13 11:01

Vladimir Berlev


2 Answers

A professional solution is not to rely on an IDE for building your software. You want to use a build tool to build the software. There are many build tools available to choose from, e.g.

  1. Make
  2. ANT
  3. Maven
  4. Rake
  5. Gradle
  6. Buildr

etc.

The tool you choose depends on how you see building software.

For example tools such as Make, ANT, etc take a “do exactly as I say” approach to building software. This can be good if you like having to spell out exactly what you want done and how you want it done.

Tools such as Maven take a “convention over configuration” approach. They say if you follow the conventions, you shouldn't have to repeatedly tell the build tool how to do standard things, so for example with Maven if you put your Java source code in the src/main/java directory, then Maven will automatically compile it for you, if you put your test source code in src/test/java then Maven will compile that for you and run all the tests.

Maven is not for everyone... some people seem to want to spend more of their time telling the build tool exactly what to do and less time writing their software... that's fine... it's not me (I am on the Maven PMC... no surprises for guessing what my favourite build tool is)... but these people do like some of the “convention over configuration” stuff that Maven gives... they just want a more flexible way of overriding when they need to step away from the convention... tools such as Gradle and Buildr are used by people with that mind-set.

Good IDEs will take the build file and infer the project structure from that build tool. IntelliJ, Eclipse and NetBeans all understand Maven's pom.xml build file. It makes getting your IDE set up a no-op. I have not investigated how the other build tools fair in IDEs as I personally use Maven and IntelliJ.

Pick a build tool and see how that works for you. I assume you are using a Version Control System... if you are then changing build tool should not be a big deal.

If you choose to use Maven, you will start with a 4 or 5 module project (a parent module and three child modules with possibly a shared common module)

The code will be structured like so

+- 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...

Your root pom.xml will look something like

<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>

And the common/pom.xml will look something like

<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>

And then each of the app poms will look something like this

<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>

Hope this helps

like image 74
Stephen Connolly Avatar answered Sep 24 '22 00:09

Stephen Connolly


I use the following approach: root-project consists of some subprojects. Some of them depends on others (this is configured in project properties) to use the code base of project depends on.

root-project
+---- project1
+---- project2
+---- project3
+---- shared-code-project
like image 27
Kerb Avatar answered Sep 23 '22 00:09

Kerb