Maven下Scala和Java混合打包

    xiaoxiao2024-12-13  17

    楔子

    Maven下Scala和Java混合打包 ,相关配置文件当独提取出来,依赖的jar包也使用meavn拷贝到一个lib目录下

    效果

    打包命令 mvn clean scala:compile compile package -- 下面这个 比上面多 dependency:copy-dependencies -DoutputDirectory=target/lib 表示吧依赖的jar 打包到 lib目录下 mvn clean scala:compile dependency:copy-dependencies -DoutputDirectory=target/lib compile package

    拷贝依赖的jar到lib目录

    mvn dependency:copy-dependencies -DoutputDirectory=target/lib

    打包 配置文件 ${project.build.directory}/config 说明

    <targetPath>${project.build.directory}/config</targetPath> 如果启用 <targetPath>${project.build.directory}/config</targetPath>那么,打包之后 jar包不会包含这些配置文件,配置文件会出现在 target\config 目录下

    pom文件

    <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 编译时的编码 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <joda-time.version>2.9.9</joda-time.version> </properties> <dependencies> <!-- 时间 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- 打包scala 这个不可少 --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.12</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>2.11.12</version> </dependency> </dependencies> <build> <resources> <!-- 解决打包时候Mapper.xml没进入 dao.class目录 (报错提示org.apache.ibatis.binding.BindingException: Invalid bound statement) --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!-- 控制资源文件的拷贝 --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/**</include> </includes> <!--<filtering>false</filtering>--> <!-- && 在IDE中运行要把这个给注释掉,因为IDEA打包 默认路劲是 classes ,有下面这行 会把配置文件转移到其他目录 --> <!-- TODO 在IDE 运行要把这个去掉 --> <targetPath>${project.build.directory}/config</targetPath> </resource> </resources> <plugins> <!-- TODO 打包命令 --> <!-- mvn clean scala:compile compile package --> <!-- mvn clean scala:compile dependency:copy-dependencies -DoutputDirectory=target/lib compile package --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!-- 编译时使用的JDK版本和UTF8编码 --> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive><!-- 清单文件 --> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.zhuzi.DemoApp</mainClass> </manifest> <manifestEntries><!-- 给清单文件添加键值对(配置文件外置) --> <Class-Path>config/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <!-- scala 打包配置 --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- 拷贝依赖的jar到lib目录(如果不配置可以使用 如下命令 把 依赖的jar 拷贝出来) --> <!-- mvn dependency:copy-dependencies -DoutputDirectory=target/lib--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 解决资源文件的编码问题 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </project>

    参考

    https://www.jianshu.com/p/9aecdfd23594 java -cp https://www.cnblogs.com/people/p/3176761.html

    使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)https://www.cnblogs.com/hdwang/p/5418747.html

    最新回复(0)