1

我是 JavaFX 的新手,我正在尝试使用 Maven 构建我的第一个应用程序。好吧,因为我不太喜欢将 ant 与 maven 混合,所以我得到了一个替代解决方案,使用exec-maven-pluginand javafxpackager,在这里找到:http ://www.oracle.com/technetwork/articles/java/enterprisefxpt3-1735081.html

问题是,我不喜欢它在 jar 中解压缩所有依赖项的方式,所以我对其进行了“一点”修改,结果如下:

<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>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>2.2</javafx.version>
    </properties>

    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>${java.home}/../bin/javafxpackager</executable>
                    <arguments>
                        <argument>-createjar</argument>
                        <argument>-appclass</argument>
                        <argument>test.HelloWorldApp</argument>
                        <argument>-srcdir</argument>
                        <argument>${project.build.directory}/classes</argument>
                        <argument>-outdir</argument>
                        <argument>${project.build.directory}/dist</argument>
                        <argument>-outfile</argument>
                        <argument>${project.name}.jar</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>javafx</artifactId>
            <version>${javafx.version}</version>
            <scope>system</scope>
            <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

嗯,我觉得真的很好看,超出预期!好吧,除了一个“小”问题:我没有类路径条目Manifest.MF,因此在运行时找不到添加的任何依赖项,我不知道如何添加它。有任何想法吗?

感谢大家的帮助!

编辑:也许这会有所帮助,javafxpackager 有一个参数classpath,我可以传递依赖项列表。所以我需要的是类路径,可能是一个字符串,只是为了将它添加到 javafxpackager。

4

2 回答 2

0

如果您查看exec-maven-pluginPOM 配置,您会发现可以添加<classpath/>标签。我没有尝试过,但它应该这样做。

<configuration>
    <executable>${java.home}/../bin/javafxpackager</executable>
    <arguments>
        <argument>-createjar</argument>
        <argument>-appclass</argument>
        <argument>test.HelloWorldApp</argument>
        <argument>-srcdir</argument>
        <argument>${project.build.directory}/classes</argument>
        <argument>-outdir</argument>
        <argument>${project.build.directory}/dist</argument>
        <argument>-outfile</argument>
        <argument>${project.name}.jar</argument>
        <argument>-classpath</argument>
        <!-- automatically creates the classpath using all project dependencies,
             also adding the project build directory -->
        <classpath/>
    </arguments>
</configuration>
于 2012-11-13T11:41:17.750 回答
0

查看此 pom.xml 以了解个人 javafx 应用程序,它不使用 javafxpackager / javapackager,但它是一个完美的可运行 jar。jar 将位于 target/${project.name}-${version}-jar-with-dependencies.jar

<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src/</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/dependency</outputDirectory>
                        <resources>
                            <resource>
                                <directory>lib</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>application.WeatherFXApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>

</build>
<dependencies>
    <dependency>
        <groupId>ant</groupId>
        <artifactId>ant-apache-log4j</artifactId>
        <version>1.6.5</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
</dependencies>

也可以使用 shade 插件获得相同的东西

<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src/</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>application.WeatherFXApplication</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>

</build>
<dependencies>
    <dependency>
        <groupId>ant</groupId>
        <artifactId>ant-apache-log4j</artifactId>
        <version>1.6.5</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
</dependencies>
于 2015-10-14T09:39:32.687 回答