简体   繁体   English

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

[英]Separate build step for Java file - Maven

Within a larger Maven project, I have a Java file (AudioFileBuilder.java) that generates audio files from the Google Cloud Text-to-Speech service.在较大的 Maven 项目中,我有一个 Java 文件 (AudioFileBuilder.java),它从 Google Cloud 文本转语音服务生成音频文件。 It can execute from its own main() class;它可以从自己的 main() class 执行; however, this file references an enumeration within the project (that other project source files depend on).但是,此文件引用项目中的枚举(其他项目源文件所依赖的)。

I am creating a fat JAR (JAR-with-dependencies), .exe file (Windows), and.app file (macOS) for the main project.我正在为主项目创建一个胖 JAR(JAR-with-dependencies)、.exe 文件(Windows)和.app 文件(macOS)。 I would like to exclude the AudioFileBuilder.java file AND the associated Google Cloud Text-to-Speech dependency in the JAR, exe, and app files.我想在 JAR、exe 和应用程序文件中排除 AudioFileBuilder.java 文件和相关的 Google Cloud Text-to-Speech 依赖项。 However, I still need the AudioFileBuilder.java file to run every time the project is built.但是,每次构建项目时,我仍然需要运行 AudioFileBuilder.java 文件。

Here is a link to my project's current POM file (the project is open-source): https://github.com/hadi16/GamesForTheBlind/blob/Alex-Branch/pom.xml这是我项目当前 POM 文件的链接(该项目是开源的): https://github.com/hadi16/GamesForTheBlind/blob/Alex-Branch/pom.xml

This is my current POM file:这是我当前的 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>

UPDATE:更新:

I divided the project into two separate modules: "audio-builder" & "application" - as suggested by Welsh.我将项目分为两个独立的模块:“audio-builder”和“application”——正如威尔士所建议的那样。

However, I have a Java enumeration ("Phrases.java") that both the audio-builder & application uses.但是,我有一个音频构建器和应用程序都使用的 Java 枚举(“Phrases.java”)。 The audio-builder needs this file to know which audio phrases are currently in the program, while the rest of the application needs this to load in said audio files & call the playback of these files.音频生成器需要这个文件来知道程序中当前有哪些音频短语,而应用程序的 rest 需要这个文件来加载所述音频文件并调用这些文件的播放。

So, this made me add the "audio-builder" module as a dependency for the "application" module.所以,这让我添加了“audio-builder”模块作为“application”模块的依赖项。 Since the Google Cloud dependency is needed at compile-time, this means that it is bundled in the generated JAR, app, and exe files.由于在编译时需要 Google Cloud 依赖项,这意味着它捆绑在生成的 JAR、app 和 exe 文件中。

These changes can be found here: https://github.com/hadi16/GamesForTheBlind/tree/Maven-Fixes这些更改可以在这里找到: https://github.com/hadi16/GamesForTheBlind/tree/Maven-Fixes

Root POM file:根 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>

Audio-builder POM file:音频生成器 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>

Application POM file (note the dependency to audio-builder):应用程序 POM 文件(注意对 audio-builder 的依赖):

<?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>

Thank you!谢谢!

I would recommend that you split your projects into two sub projects, so you would have for example:我建议您将项目分成两个子项目,例如:

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

Then what you can do is have the audio-builder package output the required files into the application/resources directory and have it listed first in the main pom.xml file.然后你可以做的是让audio-builder package output 将所需的文件放入application/resources目录中,并将其列在主pom.xml文件中。

This would allow you to distribute the application resultant files that would not contain any of the details of the audio-builder project.这将允许您分发不包含audio-builder项目的任何详细信息的application结果文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM