用Maven给一个Maven工程打包,使用阿里云镜像解决mvn clean package出错的问题,使用plugin解决没有主清单属性的问题

本来在STS里做了一个极简Maven工程,内中只有一个Main方法的Java类,然后用新装的Maven3.6.3给它打包。

结果,Maven罢工,输出如下:

复制代码
C:\personal\programs\ColoredText>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.hy:ColoredText >-------------------------
[INFO] Building ColoredText 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  22.024 s
[INFO] Finished at: 2020-03-28T09:39:17+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.5: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom: Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.52.215] failed: Connection timed out: connect -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
复制代码

本着互联网就是知识库的理念,我开启了度娘查找模式,功夫不负有心人,在度娘的帮助下,短短几秒我就找到了解决方案:

  <mirrors>
     <!--配置阿里云maven私有仓库(即配阿里私服)-->
     <mirror>
         <id>alimaven</id>
         <mirrorOf>*</mirrorOf>
         <url>https://maven.aliyun.com/repository/central</url>
     </mirror>
  </mirrors>

上图深蓝色文字就是大招,把这段文字拷贝到Maven_HOME/conf/settings.xml(在我本机是C:\maven3.6.3\conf\settings.xml)里面的mirrors节点里就好了。

注意我是在家中电脑配置mirror节点,是不需要代理就能上网的环境,诸位如果公司电脑受限还需要设置代理,要不阿里云镜像还是访问不到。

之后再打包就一路绿灯了,输出一大堆文字,我截取了最后一部分贴出来:

复制代码
us-interpolation-1.15.jar (0 B at 0 B/s)
Downloaded from alimaven: https://maven.aliyun.com/repository/central/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar (0 B at 0 B/s)
Downloaded from alimaven: https://maven.aliyun.com/repository/central/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.jar (0 B at 0 B/s)
Downloaded from alimaven: https://maven.aliyun.com/repository/central/commons-lang/commons-lang/2.1/commons-lang-2.1.jar (0 B at 0 B/s)
[INFO] Building jar: C:\personal\programs\ColoredText\target\ColoredText-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  33.497 s
[INFO] Finished at: 2020-03-28T09:45:00+08:00
[INFO] ------------------------------------------------------------------------
复制代码

 然后,我兴致勃勃地准备运行程序,结果当头被浇一盆冷水:

C:\personal\programs\ColoredText>cd target

C:\personal\programs\ColoredText\target>java -jar ColoredText-0.0.1-SNAPSHOT.jar
ColoredText-0.0.1-SNAPSHOT.jar中没有主清单属性

我又在百度上查了一下,居然要我用winrar打开jar包修改Manifest.mf文件,添加“Main-class:类名”进去,这也太麻烦了,难道每次打出包来还要手动修改一次,没道理啊,于是我又在度娘的指引下找到了解决方案:

复制代码
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- 此处为程序主入口 -->
                            <mainClass>com.ufo.hy.EntryPoint</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
复制代码

上面蓝色部分就是解决方案,设置此段到pom.xml后,指定Main-class的任务将由Maven代行,如果你的pom.xml中没有build节点你需要把整段都拷贝过去。

再次打包执行,久违的hello world就出现在控制台了。

复制代码
ompress-1.11.jar (426 kB at 91 kB/s)
[INFO] Building jar: C:\personal\programs\ColoredText\target\ColoredText-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.914 s
[INFO] Finished at: 2020-03-28T10:33:40+08:00
[INFO] ------------------------------------------------------------------------

C:\personal\programs\ColoredText>cd target

C:\personal\programs\ColoredText\target>java -jar ColoredText-0.0.1-SNAPSHOT.jar
Hello world!

C:\personal\programs\ColoredText\target>
复制代码

以上代码工程链接在此:

 https://files.cnblogs.com/files/xiandedanteng/ColoredText2020-03-28-1.zip

到此,打包并执行jar的任务就完成了。

 

喝水不忘挖井人,我找到的解决方案出处是:

https://blog.csdn.net/wudinaniya/article/details/98116734 

https://www.cnblogs.com/wayoufeidie/p/11825773.html

在此感谢两位作者的付出。

--2020年3月28日--

posted @   逆火狂飙  阅读(851)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2018-03-28 【高中数学/幂函数】y=x^2与y=x^-2 的函数图像
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示