如何使用Maven和Netbeans

时间:2016-07-06 13:19:39

标签: java maven netbeans jar executable-jar

我试图创建一个可运行的jar文件,但不知怎的,我的Maven / Netbeans没有这样做。这是我的pom.xml:

<?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>de.ksw</groupId>
    <artifactId>KBSE-CDI-Testprogram</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <version>1.8.1</version>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>KBSE-CDI</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>maven</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>1.8.1</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

正如您所看到的,我已经尝试添加主类。我还添加了依赖maven-jar-plugins,但是当我尝试编译项目时仍然收到错误消息:

  

插件org.apache.maven.plugins:maven-jar-plugin:1.8.1或其中一个依赖项无法解析:无法读取org.apache.maven.plugins的工件描述符:maven-jar-plugin :jar:1.8.1:找不到org.apache.maven.plugins:maven-jar-plugin:pom:http://repo.maven.apache.org/maven2中的1.8.1被缓存在本地存储库中,在更新之前不会重新尝试解析中心间隔已经过去或强制更新 - > [帮助1]

     

要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。   使用-X开关重新运行Maven以启用完整的调试日志记录。

     

有关错误和可能的解决方案的更多信息,请阅读以下文章:   [帮助1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

我在这里做错了什么?如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

@param range the max Manhattan distance desired @param loc a Point object @param points an array of Point objects @return number of points in the array whose Manhattan distance from the Point loc is within the specified range. public static int numWithinRange(int range, Point loc, Point[] points) { // TO DO return 0; 没有1.8.1版本。

此处列出了maven-jar-plugin的所有可用版本:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin

您必须将maven-jar-plugin更新为早期版本。

最新版本示例:

version

答案 1 :(得分:1)

我在一个项目中做的是使用maven-jar-plugin和maven-assembly-plugin

结果是一个可执行的fat-jar。

<artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
                    </manifest>
                    <manifestEntries>                      
                        <!--this one is to add the version information in the manifest. This is something that I need but you can ignore it-->  
                        <version>${project.version}</version>
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>${project.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId> 
                <classifier>DEV</classifier>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- to inheritance merges -->
                    <phase>package</phase> <!-- just to use in the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!--This plugin is used to have just one jar instead of 2-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.1</version>
            <configuration>
                <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                <finalName>${project.artifactId}</finalName>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <verify>false</verify>
            </configuration>
        </plugin>

在此之后,只需在netbeans中清理并构建按钮并搜索

  

汇编文件:PATH_TO_JAR / KBSE-CDI-Testprogram.jar