无法使用maven项目创建可运行的jar文件

时间:2014-12-08 14:33:21

标签: java spring maven

我从.NET到java的转变充满了特别是使用SPring Tool Suite的问题。我创建了一个简单的maven项目,因为每个人都告诉我这是最好的方式,因为Maven下载了所有需要的库并打包它们。在无休止的错误和修复尝试后,我让项目工作,并通过Spring以某种调试模式运行它。试图创建一个可运行的jar,这就是问题的开始。第一个jar不适用于此错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
    Caused by: org.apache.hadoop.hbase.client.NoServerForRegionException: 
    Unable to find region for  after 35 tries.
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
    locateRegionInMeta(ConnectionManager.java:1251)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
     locateRegion(ConnectionManager.java:1128)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.
    locateRegion(ConnectionManager.java:1111)
    at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion
    (ConnectionManager.java:1070)
    at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:347)
    at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:201)
    at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:159)
    at com.boardreader.hbase.HBaseMain.main(HBaseMain.java:148)
    ... 5 more

找到了一些指导我检查jre版本和jdk等的资源,并将以下内容添加到pom文件中以创建一个显示主类的清单。

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.myproject.deduper.HBaseMain</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  </plugin>
 </plugins>

然后告诉maven使用干净的包装配件构建:单个,但这不起作用。因此决定放弃并返回调试模式,但现在项目将无法运行:

cannot find main class HBaseMain

现在真的很沮丧。从pom文件中删除条目,重建和清理不起作用。调试或作为Java应用程序运行不起作用。关闭Spring然后重新打开并清理项目,最后在调试模式下工作。我需要让它工作以部署到另一台服务器,但为什么这很难实现。对于所有微软的不断抱怨,我只是试图部署一个项目没有任何问题。

编辑:

更改了插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                        <exclude>LICENSE</exclude>
                        <exclude>LICENSE.txt</exclude>
                        <exclude>NOTICE.txt</exclude>
                    </excludes>
                </filter>
            </filters>
            <transformers>
                <transformer  implementation="org.apache.maven.plugins.shade.resource.
                        ManifestResourceTransformer">
                    <mainClass>com.myproject.deduper.HBaseMain</mainClass>
                </transformer>
            </transformers>
        </configuration>
    </execution>
</executions>
 </plugin>

   

现在错误:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.338 s
[INFO] Finished at: 2014-12-08T09:54:30-05:00
[INFO] Final Memory: 13M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:
compile   (default-compile) on project Hbase-t: Fatal error compiling: tools.jar not 
found: C:\Program Files\Java\jre7\..\lib\tools.jar -> [Help 1]
[ERROR]

java_home

jre installation

2 个答案:

答案 0 :(得分:1)

将JAVA_HOME设置为指向jdk(不是jre)

JAVA_HOME=C:\jdk1.7.0_71

添加此插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.company.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

配置eclipse使用jdk:首先你必须在你的计算机上安装jdk,如果没有,请输入google&#34;下载jdk&#34;并下载它。
然后转到如下图所示的设置并单击添加... ,然后选择标准VM ,单击下一步, 并指定jdk的路径。 enter image description here

答案 1 :(得分:1)

切换到shade plugin。它比组装插件更容易使用和更好的支持。此外,它并不要求您运行单独的maven目标。它挂钩包装和安装。这应该适合你:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                            <exclude>LICENSE</exclude>
                            <exclude>LICENSE.txt</exclude>
                            <exclude>NOTICE.txt</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.x.y.z.Test</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>