英文杭州网站建设,公司注册核名查询官网,服务好的南昌网站设计,wordpress 公网访问不了实验课的作业用一大堆框架/库#xff0c;统统要用maven管理。
头一次用#xff0c;真痛苦。
所幸得以解决#xff0c;maven真香#xff5e;
一步一步来。
1. maven
不是java人#xff0c;只能说说粗浅的理解了。
简单来说#xff0c;maven是一个管理项目的工具统统要用maven管理。
头一次用真痛苦。
所幸得以解决maven真香
一步一步来。
1. maven
不是java人只能说说粗浅的理解了。
简单来说maven是一个管理项目的工具可以帮助我们下载依赖、构建项目、打包、测试。
然而需要编写恶心的pom.xml来实现。
IDEA自带的有maven但是我们需要命令行仅仅用IDEA的maven不好理解。
所以第一步就是先下载并配置maven。
下载配置maven
linux人直接apt install maven结束。
windows官网下载配配环境变量就OK了在IDEA的设置里写上你的maven可执行文件路径。 下来还需要换镜像linux在/etc/maven/setting.xml里windows下载的包里搜一下也出来了
在mirrors ... mirros中间放上阿里的镜像站 mirroridaliyunmaven/idmirrorOf*/mirrorOfname阿里云公共仓库/nameurlhttps://maven.aliyun.com/repository/public/url/mirror这份文件写满了注释有其他需要可以自己看一下比如设置仓库路径
localRepository/path/to/local/repo/localRepository默认仓库路径是{用户目录}/.m2/repository
这一步配好后你的命令行应该有mvn命令。 可以mvn --help测试一下。
2. 最小化pom.xml
为了方便理解建议命令行操作。
假设我们的项目结构是下面这样
包名为top.hello主类Hello.java的内容如下
package top.hello;
public class Hello{public String sayHello(String name){return Hello name !;}public static void main(String []args){System.out.println(Hello world);}
}按理来说下面的就够了
?xml version1.0?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtop.hello/groupIdartifactIdHello/artifactIdversion0.0.1-SNAPSHOT/version/project很简单前面的乱七八糟的链接就不要记了让插件/IDE生成官网也有。 在pom.xml同级目录下执行mvn compile来编译它会报错
$ mvn compile
...
ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] 不再支持源选项 5。请使用 7 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 7 或更高版本。
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.747 s
[INFO] Finished at: 2023-04-25T15:38:1008:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Hello: Compilation failure: Compilation failure:
[ERROR] 不再支持源选项 5。请使用 7 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 7 或更高版本。...浅搜了一下说是maven默认的jdk版本太低此时注意到了IDEA自动生成的pom.xml会有这么一段 propertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesOK把这段加到project/project下就可以了看起来意思是用jdk17。
所以真正可用的maven配置如下
?xml version1.0?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtop.hello/groupIdartifactIdHello/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project还有个nameHello/name多了少了貌似都没影响。
运行mvn compile就会生成一个target目录用命令行运行一下。 3. 可用的pom.xml
上面的已经是可以用的了但是用起来并不方便。
在IDEA里面直接点构建会报错也无法运行。
3.1 默认行为
在project下添加
builddefaultGoalpackage/defaultGoal
/build这样默认构建比如命令行输入mvn或者IDEA里点构建就会执行mvn package会生成jar包。
也可以改成 defaultGoalcompile/defaultGoal默认行为是编译。
3.2 mvn package
mvn package打包成jarjava -jar XXX.jar运行时会报错找不到MANIFEST什么的可以手动解包写一份配置文件再打包进去。
也可以在maven中设置
pluginsplugin!-- Build an executable JAR --groupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdversion3.1.0/versionconfigurationarchivemanifestaddClasspathtrue/addClasspathclasspathPrefixlib//classpathPrefixmainClasstop.hello.Hello/mainClass/manifest/archive/configuration/plugin
/plugins3.3 引入库
如果你需要引入库在project下添加 dependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.0/versionscopetest/scope/dependency/dependencies更多的库在dependencies里加更多个denpendency并填好就行了。
最终就变成这样
?xml version1.0?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtop.hello/groupIdartifactIdHello/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesnameHello/namebuilddefaultGoalpackage/defaultGoalpluginsplugin!-- Build an executable JAR --groupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdversion3.1.0/versionconfigurationarchivemanifestaddClasspathtrue/addClasspathclasspathPrefixlib//classpathPrefixmainClasstop.hello.Hello/mainClass/manifest/archive/configuration/plugin/plugins/builddependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.0/versionscopetest/scope/dependency/dependencies/project打包依赖
用的spring打包的jar运行时报错找不到spring-context。 需要打包依赖。
在build里加上下面的
pluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-dependency-plugin/artifactIdversion2.10/versionexecutionsexecutionidcopy-dependencies/idphasepackage/phasegoalsgoalcopy-dependencies/goal/goalsconfigurationoutputDirectory${project.build.directory}/lib/outputDirectory/configuration/execution/executions/plugin
/plugins然后整个build 就像下面的 builddefaultGoalpackage/defaultGoalpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-dependency-plugin/artifactIdversion2.10/versionexecutionsexecutionidcopy-dependencies/idphasepackage/phasegoalsgoalcopy-dependencies/goal/goalsconfigurationoutputDirectory${project.build.directory}/lib/outputDirectory/configuration/execution/executions/pluginplugin!-- Build an executable JAR --groupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdversion3.1.0/versionconfigurationarchivemanifestaddClasspathtrue/addClasspathclasspathPrefixlib//classpathPrefixmainClasstop.hello.TestHelloSpring/mainClass/manifest/archive/configuration/plugin/plugins/build这样打包完之后依赖在lib里运行时java -jar demo*.jar -classpath lib即可。
参考博客
Maven 最全教程看了必懂 Maven打包所有依赖到一个可执行jar中