Maven仅在使用资源插件时才在jar中编译或包含Scala .class文件

时间:2017-07-06 00:26:14

标签: scala maven jar maven-resources-plugin maven-scala-plugin

我有一个Scala应用程序,我正在尝试使用Maven打包为.jar文件。有一个application.conf文件,我试图将其作为资源打包到jar中。但是,当我使用资源插件时,或者通过将资源放在src / main / resources中自动,或者通过将其添加到某个其他文件夹的pom.xml中,Maven然后停止编译并打包jar中的.class文件。

只要我不使用资源插件,一切都可以100%运行。 Maven运行Scala编译器,将.class文件放入jar中,在通过7zip手动添加资源后,程序执行得很好。

一些额外的细节:

  • 我正在使用带有Artima Supersafe编译器插件的scala-maven-plugin
  • 我还使用maven-shade-plugin生成胖jar,连接akka reference.conf文件,并生成清单。这在使用资源插件时仍然有效,它只缺少我的 .class文件。我试过没有这个插件但是结果完全相同。
  • 我手动将sourceDirectory指定为src / main / scala
  • 当不使用资源插件时,Scala编译并且控制台输出显示[INFO] --- scala-maven-plugin:3.2.0:compile (default) @ batchmanager ---,然后显示编译器警告等。但是,当使用资源插件时,它始终坚持:[INFO] Nothing to compile - all classes are up to date
  • 使用资源插件
  • 时,application.conf文件确实正确地出现在jar中
  • 我通过Eclipse运行Maven,而不是CLI

我觉得资源插件有一些我不明白的地方。任何帮助将不胜感激。

完整的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.digitalalbatross</groupId>
    <artifactId>batchmanager</artifactId>
    <version>0.0.1</version>
    <name>${project.artifactId}</name>
    <inceptionYear>2017</inceptionYear>
    <licenses>
        <license>
            <name>My License</name>
            <url>http://....</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.12.2</scala.version>
        <scala.compat.version>2.12</scala.compat.version>
    </properties>

    <repositories>
        <repository>
            <id>artima</id>
            <name>Artima Maven Repository</name>
            <url>http://repo.artima.com/releases</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- Test -->
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http_2.12</artifactId>
            <version>10.0.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.iq80.leveldb</groupId>
            <artifactId>leveldb</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>
            <groupId>org.fusesource.leveldbjni</groupId>
            <artifactId>leveldbjni-all</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.12</artifactId>
            <version>2.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-persistence_2.12</artifactId>
            <version>2.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-stream_2.12</artifactId>
            <version>2.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-spray-json_2.12</artifactId>
            <version>10.0.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.12</artifactId>
            <version>3.0.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.12.2</version>
        </dependency>
    </dependencies>

    <build>

        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>

        <resources>
            <resource>
                <directory>res</directory>
                <includes>
                    <include>application.conf</include>
                </includes>
            </resource>
        </resources>


        <plugins>
            <plugin>
                <!-- see http://davidb.github.com/scala-maven-plugin -->
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <compilerPlugins>
                                <compilerPlugin>
                                    <groupId>com.artima.supersafe</groupId>
                                    <artifactId>supersafe_${scala.version}</artifactId>
                                    <version>1.1.2</version>
                                </compilerPlugin>
                            </compilerPlugins>
                            <args>

                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <!-- If you have classpath issue like NoDefClassError,... -->
                    <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>



            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>allinone</shadedClassifierName>
                            <artifactSet>
                                <includes>
                                    <include>*:*</include>
                                </includes>
                            </artifactSet>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.digitalalbatross.batchmanager.Boot</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
    <description>Batch Manager for IMAX</description>
    <organization>
        <name>Digital Albatross Design and Consulting</name>
        <url>digitalalbatross.com</url>
    </organization>
</project>

1 个答案:

答案 0 :(得分:0)

I managed to solve my issue in the course of solving another. My eclipse workbench kept crashing on boot, so I deleted the .plugins folder to try and solve the issue. Not only did this fix my eclipse setup, but Maven decided to cooperate and started to actually include both resource and class files in the jar. After some confusion over which of the generated jar files actually had a working manifest, I've verified that it is now working as expected.

To summarize, it was most likely some bizarre bug caused by something in my .plugins folder for eclipse. Deleting that folder and reconfiguring my setup fixed the issue.