maven项目,多个依赖,打成一个可执行jar包,可根据profiles进行打包,出现的Could not find or load main class的解决方法。

3 篇文章 0 订阅

maven项目,多个依赖,打成一个可执行jar包,可根据profiles进行打包,出现的Could not find or load main class的解决方法。 


本人的项目是一个设备端运行的项目,项目创建初期,使用的是简单的java项目,需要自己变异成可执行jar包运行。


后期,为了自动编译,将其改为maven项目。

因为需要将所要的maven依赖打进一个jar包,使用了maven-assembly-plugin的方式。
因为项目的源码的位置是src/com/ihangmei/dmc
因此,build中重新指定了源码路径。编译后,能够成功执行。
<build>
<!-- mvn打包的默认路径是src/main/java -->
        <!-- sourceDirectory 本项目在src下,打包出现:skip non existing resourceDirectory
            ../heartbeatProject/src/main/resources -->
            <sourceDirectory>src</sourceDirectory>
            。。。。
    </build>            

其中,finalName是编译后的jar的名称。

此时,build的完整配置如下:

    <build>
        <!-- mvn打包的默认路径是src/main/java -->
        <!-- sourceDirectory 本项目在src下,打包出现:skip non existing resourceDirectory
            ../heartbeatProject/src/main/java -->
                    <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.ihangmei.dmc.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 将依赖包打到一个文件中 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <finalName>1231</finalName>
                    <archive>
                        <manifest>
                            <mainClass>com.ihangmei.dmc.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
    
    ----
    
    再pom文件中增加了开发,测试,正式的profile配置信息,因为增加了properties文件,
    
    配置文件变为了
    <build>
        <!-- mvn打包的默认路径是src/main/java -->
        <!-- sourceDirectory 本项目在src下,打包出现:skip non existing resourceDirectory
            ../heartbeatProject/src/main/resources -->
        <resources>
            <resource>
                <directory>src</directory>
                <!-- 将pom中的profile的属性值放在properties文件中 -->
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.ihangmei.dmc.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 将依赖包打到一个文件中 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <finalName>1231</finalName>
                    <archive>
                        <manifest>
                            <mainClass>com.ihangmei.dmc.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <!-- 本地开发环境(Development) -->
        <profile>
            <id>Development</id>
            <!-- 打包时不指定profile id,默认激活本环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
            </properties>
        </profile>
        <!-- 测试环境(TEST) 提供给测试团队的 -->
        <profile>
            <id>Test</id>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
            </properties>
        </profile>
        <!-- 正式生产环境(Production) -->
        <profile>
            <id>Production</id>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
            </properties>
        </profile>
    </profiles>
    
    此时,打包时,即使<directory>src</directory>指定了源码路径,
    编译完成后,使用java -jar XXX.jar 或者java -cp XXX.jar com.ihangmei.dmc.Heartbeat,
    解释是找不到main方法。
    
    经过检查发现,XXX.jar包中,保存的文件是.java,不是.class文件。
    
    因此,继续检查编译时的源码路径
    使用mvn clean compile -X,查看编译时的输出,
    发现,    No sources to compile
    ,继续追踪,发现compileSourceRoots = [D:\Users\zp\git\heartbeatProject\src\main\java]
    
    分析原因:
    将<resources>
            <resource>
                <directory>src</directory>
                <!-- 将pom中的profile的属性值放在properties文件中 -->
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        中的resource--directory
        与<sourceDirectory>src</sourceDirectory> 认为是相同的。
        其实是不同的。
        将pom文件中增加<sourceDirectory>src</sourceDirectory>,继续编译,mvn clean compile -X,通过,
        源码路径:[DEBUG]   (f) compileSourceRoots = [D:\Users\zp\git\heartbeatProject\src]
        
    
    正确的配置如下
    
    <build>
        <!-- mvn打包的默认路径是src/main/java -->
        <!-- sourceDirectory 本项目在src下,打包出现:skip non existing resourceDirectory
            ../heartbeatProject/src/main/resources -->
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <!-- 将pom中的profile的属性值放在properties文件中 -->
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>dmc.ihangmei.com.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 将依赖包打到一个文件中 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <finalName>1231</finalName>
                    <archive>
                        <manifest>
                            <mainClass>dmc.ihangmei.com.Heartbeat</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <!-- 本地开发环境(Development) -->
        <profile>
            <id>Development</id>
            <!-- 打包时不指定profile id,默认激活本环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
                <hbServer>http://139.217.21.195:16524</hbServer>
                <!-- 独立日志服务器 -->
                <privateLogServer>http://139.217.24.148:6620</privateLogServer>
                <!-- 使用域名的方式 UC正式服务器地址uc.amol.com.cn -->
                <UCServerAddress>uc.amol.com.cn</UCServerAddress>
                <!-- uc.amol.com.cn对应的IP -->
                <UCServerAddressIP>139.217.19.34</UCServerAddressIP>
                <!-- 分钟数,到时间就自杀,保证内存泄露(如果有)不会影响系统 -->
                <liveTime>30</liveTime>
                <!-- 秒数,最短心跳间隔 -->
                <minInterval>60</minInterval>
                <!-- 秒数,设备访问GEOService的间隔 -->
                <geoInterval>320</geoInterval>
                <!-- 日志上传服务器 -->
                <uploadURL>http://app.iwangfan.cn:8997/datapro-receive/transfer.do</uploadURL>
                <!-- 要压缩的文件大小,要大于200KB -->
                <tarSize>204800</tarSize>
                <!-- 上传日志的时间间隔 ,单位毫秒,30分钟 -->
                <uploadInterval>1800000</uploadInterval>
                <!-- 检查需要上传日志的时间间隔 ,单位毫秒,1分钟 -->
                <checkInterval>60000</checkInterval>
                <!-- 打点日志保存时间的间隔 ,单位毫秒,10秒 -->
                <hitlogSaveInterval>10000</hitlogSaveInterval>
            </properties>
        </profile>
        <!-- 测试环境(TEST) 提供给测试团队的 -->
        <profile>
            <id>Test</id>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
                <hbServer>http://139.217.26.111:8081/heartbeat/</hbServer>
                <!-- 独立日志服务器 -->
                <privateLogServer>http://139.217.24.148:6620</privateLogServer>
                <!-- 使用域名的方式 UC正式服务器地址uc.amol.com.cn -->
                <UCServerAddress>uc.test.amol.com.cn</UCServerAddress>
                <!-- uc.amol.com.cn对应的IP -->
                <UCServerAddressIP>139.217.26.25</UCServerAddressIP>
                <!-- 分钟数,到时间就自杀,保证内存泄露(如果有)不会影响系统 -->
                <liveTime>30</liveTime>
                <!-- 秒数,最短心跳间隔 -->
                <minInterval>60</minInterval>
                <!-- 秒数,设备访问GEOService的间隔 -->
                <geoInterval>320</geoInterval>
                <!-- 日志上传服务器 -->
                <uploadURL>http://dmc-tst-wifibox.chinacloudapp.cn:8080/datapro-receive/transfer.do</uploadURL>
                <!-- 要压缩的文件大小,要大于512Byte -->
                <tarSize>512</tarSize>
                <!-- 上传日志的时间间隔 ,单位毫秒,10秒 -->
                <uploadInterval>10000</uploadInterval>
                <!-- 检查需要上传日志的时间间隔 ,单位毫秒,10秒 -->
                <checkInterval>10000</checkInterval>
                <!-- 打点日志保存时间的间隔 ,单位毫秒,10秒 -->
                <hitlogSaveInterval>10000</hitlogSaveInterval>
            </properties>
        </profile>
        <!-- 正式生产环境(Production) -->
        <profile>
            <id>Production</id>
            <properties>
                <!-- 心跳服务器 URL, 最重要的配置, 域名是: hb.ihangmei.com -->
                <hbServer>http://139.217.21.195:16524</hbServer>
                <!-- 独立日志服务器 -->
                <privateLogServer>http://139.217.24.148:6620</privateLogServer>
                <!-- 使用域名的方式 UC正式服务器地址uc.amol.com.cn -->
                <UCServerAddress>uc.amol.com.cn</UCServerAddress>
                <!-- uc.amol.com.cn对应的IP -->
                <UCServerAddressIP>139.217.19.34</UCServerAddressIP>
                <!-- 分钟数,到时间就自杀,保证内存泄露(如果有)不会影响系统 -->
                <liveTime>30</liveTime>
                <!-- 秒数,最短心跳间隔 -->
                <minInterval>60</minInterval>
                <!-- 秒数,设备访问GEOService的间隔 -->
                <geoInterval>320</geoInterval>
                <!-- 日志上传服务器 -->
                <uploadURL>http://app.iwangfan.cn:8997/datapro-receive/transfer.do</uploadURL>
                <!-- 要压缩的文件大小,要大于200KB -->
                <tarSize>204800</tarSize>
                <!-- 上传日志的时间间隔 ,单位毫秒,30分钟 -->
                <uploadInterval>1800000</uploadInterval>
                <!-- 检查需要上传日志的时间间隔 ,单位毫秒,1分钟 -->
                <checkInterval>60000</checkInterval>
                <!-- 打点日志保存时间的间隔 ,单位毫秒,10秒 -->
                <hitlogSaveInterval>10000</hitlogSaveInterval>
            </properties>
        </profile>
    </profiles>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值