我建立了一个类似的模型,你可以学习。
通过使用 Maven,它将尽可能地与 IDE 无关。您不必在 VCS 中存储任何特定的 IDE 设置,只需存储源代码和 pom 文件即可。每个开发人员将启动他的 IDE 并指向顶部的 pom,并且项目应该加载。将创建本地设置,但在提交到 VCS 时应忽略。
首先,多模块 Maven 项目的布局肯定与 C# 解决方案及其项目非常相似。具有 parent-pom 的顶级文件夹将类似于具有共享配置和构建顺序等的解决方案。然后具有子 pom 的子文件夹将匹配项目定义与其他项目之间的依赖关系。
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());
}
}
这是我测试过的。当然可以对 pom 文件和依赖项进行一些改进,但这是一个好的开始。
如果您查看,web/target
您会发现webapp.war
其中包含所需的依赖项。
当然可以将所有这些模块拆分并单独构建它们,并且它们之间仍然存在依赖关系,但正如我所说,这是一个很好的起点。