How to organize a Java solution in several projects, for example, in Visual Studio?

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

"Solutions" is a useful concept in Visual Studio. How can I break a Java solution into β€œprojects” (I think Java packages) that have interdependencies in the same source control repository?

We plan to use Maven for third-party dependencies and Scala for writing some libraries.

We must also be independent of the IDE.

Recommendations are gratefully received!

Edit: Assume that the solution will contain a web application, a console application, and a library written in Scala.

+7
source share
7 answers

I created a similar model that you can study.

Using Maven, he will be an IDE agnostic as soon as possible. You will not need to store any specific IDE settings in your VCS, only the source code and 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 transferring to VCS.

First of all, a Maven project with several modules will definitely have a very similar layout, like a C # solution with its projects. The top folder with the parent pom will look like a solution with general configurations and build order, etc. Subfolders with sub-pumps will then correspond to 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()); } } 

This has been verified by me. Of course, there may be some improvements in pom files and dependencies, but this is a good start.

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

Of course, you can break down all these modules and build them separately and still have dependencies between them, but, as I said, this is a good starting point.

+9
source

My personal approach is to use Maven for both:

  • Third-party dependencies (usually through Maven Central)
  • Interdependencies between different projects (usually through a local repository)

This works very well and I believe it is independent of the IDE (although I 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 common repository containing them, although this can be done quite simply, for example:

+2
source

Using gradle , you have to create one superproject and create your various modules as subprojects. From there, you can create workspace / project descriptions for various IDEs.

Since gradle is fully compatible with Maven style repositories, @mikera notes are also stored in repositories.

+2
source

Your C # solution depends on Visual Studio, as the Java solution will depend on its tools.

The answer also depends on the packaging: JAR for desktop computers, WAR for the Internet, EAR for enterprises with EJB.

Maven makes many options for you if you decide to go in this direction (for example, directory structure, etc.).

Eclipse and IntelliJ will do it differently. IntelliJ uses the module concept, which I find works very well. I prefer it over Eclipse; I am not familiar with how this is done in this development environment.

+1
source

Use the Maven pom-packaging project in the root of your project structure to create all sources using Maven.

Then use workspaces (Eclipse) or groups (NetBeans) to display a subset of the projects you want to work with.

+1
source

In fact, you are creating different projects that will be deployed as different jar files; then using Maven, these dependencies can be added where they are needed.

Maven also provides independence from the IDE; There are plugins for different IDEs. For example,

 mvn idea:idea 

Maven automatically creates project files for the Intellij IDE.

0
source

The solution is a property of Visual Studio, not .Net. In the future, eclipse may support the solution (possibly).

You can emulate the idea of ​​a solution using the WORKSPACE concept in Eclipse

  • Create a new workspace
  • Import all projects (make sure you check "Copy project to workspace to create a physical copy)
  • Now, when you open this workspace, you can always see the whole project.

You can also link to other projects in the workspace using Build-> Configure Build Path β†’ Project and Add Projects

If you want to "Exclude from solution", you can do the same in eclipse by "Closing the project"

-one
source

All Articles