gpt4 book ai didi

java - Java 文件的单独构建步骤 - Maven

转载 作者:行者123 更新时间:2023-12-01 21:10:38 25 4
gpt4 key购买 nike

在一个较大的 Maven 项目中,我有一个 Java 文件 (AudioFileBuilder.java),它从 Google Cloud 文本转语音服务生成音频文件。它可以从自己的 main() 类执行;但是,此文件引用项目内的枚举(其他项目源文件依赖于此)。

我正在为主项目创建一个 fat JAR(带依赖项的 JAR)、.exe 文件 (Windows) 和 .app 文件 (macOS)。我想排除 AudioFileBuilder.java 文件以及 JAR、exe 和应用程序文件中关联的 Google Cloud 文本转语音依赖项。但是,我仍然需要 AudioFileBuilder.java 文件在每次构建项目时运行。

这里是我的项目当前 POM 文件的链接(该项目是开源的): https://github.com/hadi16/GamesForTheBlind/blob/Alex-Branch/pom.xml

这是我当前的 POM 文件:

<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>edu.up</groupId>
<artifactId>Games_For_Blind</artifactId>
<version>1.0.0.0</version>

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

<packaging>jar</packaging>

<dependencies>
<!-- Compile Dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.117.0-beta</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>gamesforblind.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<!-- To generate the phrase audio files. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>

<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>gamesforblind.synthesizer.AudioFileBuilder</argument>
</arguments>

<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>google_api_key.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>

<!-- Create executable Windows file. -->
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<jar>${project.build.directory}/dist/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
<outfile>${project.build.directory}/dist/${project.artifactId}-${project.version}.exe</outfile>
<classPath>
<mainClass>gamesforblind.Main</mainClass>
</classPath>
<jre>
<minVersion>12</minVersion>
<jdkPreference>preferJre</jdkPreference>
</jre>
<versionInfo>
<copyright>2019</copyright>
<fileVersion>${project.version}</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<productVersion>${project.version}</productVersion>
<txtProductVersion>${project.version}</txtProductVersion>
<productName>${project.name}</productName>
<internalName>${project.name}</internalName>
<originalFilename>${project.artifactId}-${project.version}.exe</originalFilename>
<companyName>University of Portland</companyName>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>de.perdian.maven.plugins</groupId>
<artifactId>macosappbundler-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<plist>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<JVMMainClassName>gamesforblind.Main</JVMMainClassName>
<JVMVersion>12</JVMVersion>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<CFBundleName>Games For The Blind</CFBundleName>
</plist>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

更新:

我将项目分为两个独立的模块:“音频构建器”和“应用程序” - 正如威尔士所建议的。

但是,我有一个音频生成器和应用程序都使用的 Java 枚举(“Phrases.java”)。音频生成器需要此文件来了解程序中当前有哪些音频短语,而应用程序的其余部分需要此文件来加载所述音频文件并调用这些文件的播放。

所以,这让我添加了“audio-builder”模块作为“application”模块的依赖项。由于编译时需要 Google Cloud 依赖项,这意味着它捆绑在生成的 JAR、应用程序和 exe 文件中。

这些更改可以在这里找到:https://github.com/hadi16/GamesForTheBlind/tree/Maven-Fixes

根 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>edu.up</groupId>
<artifactId>games-for-the-blind</artifactId>
<version>1.0.0.0</version>

<modules>
<module>games-for-the-blind/audio-builder</module>
<module>games-for-the-blind/application</module>
</modules>

<packaging>pom</packaging>

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

音频构建器 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>edu.up</groupId>
<artifactId>audio-builder</artifactId>
<version>1.0.0.0</version>

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

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.117.0-beta</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>

<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>builder.AudioFileBuilder</argument>
</arguments>

<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>google_api_key.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>

应用程序 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>edu.up</groupId>
<artifactId>application</artifactId>
<version>1.0.0.0</version>

<packaging>jar</packaging>

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

<dependencies>
<!-- Compile Dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>edu.up</groupId>
<artifactId>audio-builder</artifactId>
<version>1.0.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>gamesforblind.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<!-- Create executable Windows file. -->
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<jar>
${project.build.directory}/dist/${project.artifactId}-${project.version}-jar-with-dependencies.jar
</jar>
<outfile>${project.build.directory}/dist/${project.artifactId}-${project.version}.exe
</outfile>
<classPath>
<mainClass>gamesforblind.Main</mainClass>
</classPath>
<jre>
<minVersion>12</minVersion>
<jdkPreference>preferJre</jdkPreference>
</jre>
<versionInfo>
<copyright>2019</copyright>
<fileVersion>${project.version}</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<productVersion>${project.version}</productVersion>
<txtProductVersion>${project.version}</txtProductVersion>
<productName>${project.name}</productName>
<internalName>${project.name}</internalName>
<originalFilename>${project.artifactId}-${project.version}.exe</originalFilename>
<companyName>University of Portland</companyName>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>de.perdian.maven.plugins</groupId>
<artifactId>macosappbundler-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<plist>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<JVMMainClassName>gamesforblind.Main</JVMMainClassName>
<JVMVersion>12</JVMVersion>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<CFBundleName>Games For The Blind</CFBundleName>
</plist>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

谢谢!

最佳答案

我建议您将项目拆分为两个子项目,例如:

games-for-the-blind/
pom.xml
audio-builder/
pom.xml
application/
pom.xml

然后您可以做的是让 audio-builder 包将所需的文件输出到 application/resources 目录中,并将其首先列在主 中pom.xml 文件。

这将允许您分发不包含 audio-builder 项目的任何详细信息的 application 结果文件。

关于java - Java 文件的单独构建步骤 - Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com