本文介绍了使用javafxpackager在JavaFX上的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,我正在尝试使用Maven构建我的第一个应用程序.好吧,由于我真的不喜欢将ant与maven混合使用,因此我找到了使用exec-maven-pluginjavafxpackager的替代解决方案,位于以下位置: http://www.oracle.com/technetwork/articles/java/enterprisefxpt3-1735081.html

I'm new to JavaFX and I'm trying to build my first application, using Maven. Well, since I don't really like to mix ant with maven, I got an alternative solution, using exec-maven-plugin and javafxpackager, found here: http://www.oracle.com/technetwork/articles/java/enterprisefxpt3-1735081.html

问题是,我不喜欢它在jar内解压缩我所有依赖项的方式,因此我对其进行了小修改",结果是:

Problem is, I don't like the way it unpacks all my dependencies inside the jar, so I modified it a "little", resulting on this:

<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上没有classpath条目,因此在运行时找不到添加的任何依赖项,而且我也不知道如何添加它.有什么想法吗?

Well, I think it really looks good, more than expected! Well, all but one "little" problem: I don't have the classpath entry on the Manifest.MF, so any dependencies added aren't found on runtime, and I have no clue how to add it. Any ideas?

谢谢大家的帮助!

也许可以帮上忙,javafxpackager有一个参数classpath,我可以传递依赖项列表.因此,我需要的是类路径,也许作为字符串,只是将其添加到javafxpackager中.

Maybe this can help, javafxpackager has an argument classpath that I can pass a list of dependencies. So what I need is the classpath, maybe as a String, just to add it to javafxpackager.

推荐答案

签出此pom.xml以获取个人javafx应用程序它不使用javafxpackager/javapackager,而是一个完美的可运行jar. jar将位于target/$ {project.name}-$ {version} -jar-with-dependencies.jar

check out this pom.xml for a personal javafx applicationit does not use javafxpackager / javapackager, but is a perfect runnable jar. jar will be under 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>

也可以通过阴影插件获得相同的东西

also is possible to obtain the same thing with shade plugin

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

这篇关于使用javafxpackager在JavaFX上的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:13