Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you organise a Java solution into multiple projects, like in Visual Studio?

I'm a .NET developer who needs to use the Java platform for a new project.

"Solutions" are a useful Visual Studio concept. How do I break my Java solution into "projects" (I guess Java packages) which have build interdependencies in the same source control repository?

We're planning to use Maven for third-party dependencies, and Scala for writing some of the libraries.

We also need to be IDE-independent.

Recommendations gratefully received!

Edit: Let us assume the solution will contain a web application, a console application, and a library written in Scala.

like image 380
Pete Montgomery Avatar asked Jun 27 '12 12:06

Pete Montgomery


2 Answers

I have set up a similar model that you can study.

By using Maven it will be as IDE-agnostic as it can. You will not have to store any specific IDE settings in your VCS, just the source code and the pom files. Each developer will launch his IDE and point to the top pom and the project should load. Local settings will be created but should be ignored when committing to the VCS.

First of all a multi module Maven project will most definitely have a very similar layout as a C# solution with its projects. The top-folder with the parent-pom will be like the solution with the shared configurations and build order etc. Then the sub-folders with the sub-poms will match the project definitions with dependencies between other projects.

directory layout

+- pom.xml
+- scala
| +- pom.xml
| +- src
|   +- main
|     +- scala
+- console
| +- pom.xml
| +- src
|   +- main
|     +- java
+- web
  +- pom.xml
  +- src
    +- main
     +- java
     +- resources
     +- webapp
       +- WEB-INF
         -- web.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>Q11226363</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <scala.version>2.9.2</scala.version>
    </properties>

    <modules>
        <module>scala</module>
        <module>web</module>
        <module>console</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <!-- Inter-Module dependencies -->
            <dependency>
                <groupId>com.stackoverflow</groupId>
                <artifactId>Q11226363-scala</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>${scala.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>

</project>

scala/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q11226363</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q11226363-scala</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <phase>test-compile</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

console/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q11226363</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q11226363-console</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q11226363-scala</artifactId>
        </dependency>
    </dependencies>

</project>

web/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q11226363</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q11226363-web</artifactId>
    <packaging>war</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q11226363-scala</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>webapp</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>

</project>

scala/src/main/scala/com/stackoverflow/Q11226363/ScalaApp.scala

/**
 * @author maba, 2012-06-28
 */
package com.stackoverflow.Q11226363

class ScalaApp {
  def helloScala():String = "Hello from Scala!"
}

console/src/main/java/com/stackoverflow/Q11226363/JavaApp.java

package com.stackoverflow.Q11226363;

/**
 * @author maba, 2012-06-28
 */
public class JavaApp {
    public static void main(String[] args) {
        ScalaApp scalaApp = new ScalaApp();
        System.out.println("Scala says: " + scalaApp.helloScala());
    }
}

That has been tested by me. There can of course be some improvements to the pom files and dependencies but it is a good start.

If you look in the web/target you will find your webapp.war which will include the dependencies needed.

It is of course possible to split all these modules up and build them separately and still have dependencies between them but as I said it is a good starting point.

like image 112
maba Avatar answered Oct 26 '22 01:10

maba


My personal approach is to use Maven for both:

  • Third-party dependencies (typically via Maven Central)
  • Inter-dependencies between different projects (typically via the local repository)

This works pretty well and I believe it is IDE independent (although I've only tested it on Eclipse)

Note:

If you have a team working together on the same artifacts you will need some way to have a shared repository containing them, although this can be done quite simply by e.g.:

  • Putting a repository on a shared file server (see: http://maven.apache.org/guides/introduction/introduction-to-repositories.html)
  • Hosting a repository on GitHub (see: http://cemerick.com/2010/08/24/hosting-maven-repos-on-github/)
like image 35
mikera Avatar answered Oct 26 '22 02:10

mikera