最近的项目想换java来写了,然后就疯狂踩不少雷,整理一下遇到的问题
Maven的搭建:
Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具
下载:http://maven.apache.org/download.cgi
下载bin.zip
解压到指定目录如:E:\apache-maven-3.6.1-bin
(底下有root,bin等文件夹)
配置环境变量:
新建 M2_HOME 值:E:\apache-maven-3.6.1-bin
修改path变量,在原值之后添加%M2_HOME%\bin;
检查配置:
mvn -v
配置Maven本地仓库
打开E:\apache-maven-3.6.1-bin\conf\settings\xml
搜索:
/path/to/local/repo
此配置值放在<!–> </!–>注释下,将其挪出注释外并改为指定目录如:E:\pyc_javaWorkSpace\javaPack\maven-repository
运行mvn help:system
完成之后该目录底下会出现文件
配置Eclipse的Maven环境
步骤不算繁琐,不过也懒得贴图了,参考教程:https://www.cnblogs.com/eagle6688/p/7838224.html
Maven编译的jdk版本很低,不指定版本很多写法不支持
指定默认编译jdk版本:
在Maven的settings中增加:(搜索关键字“profile”能找到相关使用示例,包括此节点放在哪个根节点下等)
<profile>
<id>jdk11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
(我用的jdk版本是11)
另一种指定jdk版本的方式是在pom.xml根节点(project)底下直接添加以下这段:
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
将java项目转Maven项目:
右键选择 JavaDemo,
选择“Configure –> Convert to Maven Project”,
填写“Group ID”和“Artifact Id”都为“JavaDemo(自定义)”。
项目根目录下多出一个pom.xml文件
pom.xml:
依赖 举例:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
要用什么就往dependencies节点底下添加dependency节点
尽量不要在classpath下导入jar包,因为不是maven管理的相关jar包编译的时候不会打包进去
Maven项目编译发布:
1.指定资源,并且指定将以下资源打包到jar包内
2.将依赖到的包打到jar包内(使用maven-assembly-plugin 插件)
<build>
<finalName>Yiri_Analyzer</finalName>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**/*.txt</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId> maven-assembly-plugin </artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.outputDirectory}</classpathPrefix>
<mainClass>com.kjtxanalyzerj.entrance</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
alt+F5->将Pom文件应用到Maven项目中
选中项目,右键->Run as-> Maven Clean 清理项目后
右键->Run as -> Maven Build 生成jar包
生成之后在项目底下会出现target文件夹
底下出现这个xxx-jar-with-dependencies.jar
目前就生成完成了
生成完成后,不要急着在windows系统内发布服务,先测试此jar包是否可用
右键->新建txt,改后缀为bat,编辑内容:
E:
cd 路径
java -jar XXXX-jar-with-dependencies.jar
Pause
保存bat文件后,右键以管理员身份运行
如上图,是可以使用的
然后接下来将其发布成windows服务
winSW:
下载地址:https://github.com/kohsuke/winsw/releases
将下载的WinSW.NET4.exe文件复制到jar程序所在文件夹中。
将WinSW.NET4.exe重命名为XXX.exe(可以和jar同名,也可不同命)。
在同目录下新建一个xml文件,命名为XXX.xml(必须和exe文件同名)
如下图:
xml文件内容:
<service>
<id>Yiri_Analyzer</id><!-- 和exe同名-->
<name>Yiri_Analyzer</name><!-- 服务名称-->
<description>Yiri_Analyzer service.</description>
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<!--<executable>%BASE%\jre\bin\java</executable>-->
<!-- 环境配置,考虑没装jre的电脑时,可将jre拷贝至某个路径并指定,注意jre不完整少了部分jar包可能会导致服务报错-->
<arguments>-jar "C:\xxx\xxx-jar-with-dependencies.jar"</arguments>
<!-- 指定jar路径-->
<startmode>Automatic</startmode>
<!-- 开机启动-->
<logpath>%BASE%\servicelog</logpath>
<logmode>rotate</logmode>
</service>
安装和卸载服务:
新建两个bat,右键以管理员身份运行
完成
如何添加依赖?
有时使用某个框架,直接把网上的代码贴上
发现import时报错,但是又不知道添加什么依赖jar包,可以直接去这个网站查询:
https://mvnrepository.com/
举例:
想用WebService时,
import javax.jws.WebService;
报了这个错
可以直接在上面搜索javax.jws
dependency在上面基本都有,直接拷贝即可