使用Java和Scala源打包WAR

时间:2013-06-20 07:45:01

标签: scala java-ee maven-3 war maven-plugin

我想问你一个简单的问题。是否可以打包包含Scala和Java源的WAR文件,每个源文件来自不同的源目录(src / main / java / ...和src / main / scala / ...)?当然,我仍然希望将其他资源(如图像和静态页面)包含在生成的WAR中。

我一直在研究WAR插件功能,关于其他来源,但我在这方面没有找到任何帮助。任何人都可以给我一个提示,我应该在哪里寻找解决方案,或者可能有任何帮助?

谢谢, 马修

1 个答案:

答案 0 :(得分:2)

这是我用于多语言项目的pom的缩减版本。

<?xml version="1.0" encoding="UTF-8"?>
<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.sksamuel</groupId>
    <artifactId>test</artifactId>
    <version>0.33-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <maven.plugin.jar.version>2.4</maven.plugin.jar.version>
        <maven.plugin.war.version>2.3</maven.plugin.war.version>
        <maven.plugin.resources.version>2.6</maven.plugin.resources.version>
        <maven.plugin.compiler.version>3.1</maven.plugin.compiler.version>
        <maven.plugin.surefire.version>2.14.1</maven.plugin.surefire.version>
        <maven.plugin.dependency.version>2.4</maven.plugin.dependency.version>
        <maven.plugin.scala.version>3.1.5</maven.plugin.scala.version>

        <compiler.version.source>1.7</compiler.version.source>
        <compiler.version.target>1.7</compiler.version.target>
    </properties>

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

    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven.plugin.war.version}</version>
                </plugin>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>${maven.plugin.scala.version}</version>
                    <executions>
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>add-source</goal>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>scala-test-compile</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在该目录中运行mvn clean包后,在src / main / scala中使用scala源,在src / main / java中使用java源文件,这就是我得到的。正如你所看到的那样,scala和java文件都被编译成了战争。

$ find .
.
./pom.xml
./target
./target/test
./target/test/WEB-INF
./target/test/WEB-INF/lib
./target/test/WEB-INF/lib/scala-library-2.10.2.jar
./target/test/WEB-INF/classes
./target/test/WEB-INF/classes/ILoveJava.class
./target/test/WEB-INF/classes/ILoveScala$.class
./target/test/WEB-INF/classes/ILoveScala.class
./target/test/WEB-INF/web.xml
./target/test/META-INF
./target/classes.-1172594870.timestamp
./target/maven-archiver
./target/maven-archiver/pom.properties
./target/test.war
./target/classes
./target/classes/ILoveJava.class
./target/classes/ILoveScala$.class
./target/classes/ILoveScala.class
./target/surefire
./src
./src/main
./src/main/webapp
./src/main/webapp/WEB-INF
./src/main/webapp/WEB-INF/web.xml
./src/main/java
./src/main/java/ILoveJava.java
./src/main/scala
./src/main/scala/ilovescala.scala