‘壹’ maven 中的jetty是怎么启动
1.法1: 在pom中配置
<build>
<finalName>testdemo</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<!-- 在很短的时间间隔内在扫描web应用检查是否有改变,如果发觉有任何改变则自动热部署。默认为0,表示禁用热部署检查。任何一个大于0的数字都将表示启用。 -->
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<!--jetty插件启动后的访问路径: http://localhost:8080/testdemo-->
<contextPath>/testdemo</contextPath>
<tempDirectory>${project.build.directory}/work</tempDirectory>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
在命令行中输入如下命令:
a.切换的项目坐在文件夹
d:
cd workspace/testdemo
b.清理、打包
mvn clean
或
mvn clean install -Dmaven.test.skip=true
c.在jetty中运行(这个定位到src/main/webapp目录,只执行mvn clean则target下不会生成war包)
mvn jetty:run
d.访问http://localhost:8080/testdemo
e.停止jetty
ctrl+c
y
=====================================================
2.法2: 在pom中配置
<build>
<finalName>testdemo</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<contextPath>/testdemo</contextPath>
<webApp>${basedir}/target/testdemo.war</webApp>
</configuration>
</plugin>
</plugins>
</build>
在命令行中输入如下命令:
a.切换的项目坐在文件夹
d:
cd workspace/testdemo
b.打包
mvn clean
c.在jetty中运行
mvn jetty:run-war -Dmaven.test.skip=true
d.访问http://localhost:8080/testdemo
e.停止jetty
ctrl+c
y
=====================================================
3.法3: 在pom中配置
<build>
<finalName>testdemo</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<contextPath>/testdemo</contextPath>
<war>${basedir}/target/testdemo.war</war>
</configuration>
</plugin>
</plugins>
</build>
在命令行中输入如下命令:
a.切换的项目坐在文件夹
d:
cd workspace/testdemo
b.打包
mvn clean
c.在jetty中运行
mvn jetty:run-war -Dmaven.test.skip=true
d.访问http://localhost:8080/testdemo
e.停止jetty
ctrl+c
y
eclipse如何进断点调试(前提是eclipse安装了maven插件,工程由maven创建,且工程pom.xml中配置了如上所说的jetty插件):
1. 工程右键-->调试方式-->调试配置-->maven build
2. Maven Build-->新建-->右侧:main-->Base Directory-->Browse Workspace-->点选要调试的工程-->Goals:clean jetty:run-->Skip Tests-->应用-->调试(启动过程中会自仓库下载依赖的插件,注意仓库<repositories>配置)
3. 访问url操作
4. 进断点后,如果报找不到源,则点击"编辑源查找路径"-->"添加"-->java 项目-->确定-->勾选要调试项目-->确定-->确定-->进入断点
‘贰’ maven+tomcat调试下,如何做到不用每次都打包,而是直接启动服务就ok
<build>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>-lib-src-webapps</id>
<phase>package</phase>
<configuration>
<tasks>
<delete dir="src/main/webapp/WEB-INF/lib" />
< todir="src/main/webapp/WEB-INF/lib">
<fileset dir="target/sem-tools-0.1/WEB-INF/lib">
<include name="*" />
</fileset>
</>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
webRoot设置为src/main/webapp
这样只需要在依赖有变化时package一次
‘叁’ 有用过maven 的吗,感觉在maven管理下 调试有点麻烦 每次改一点东西都要重新 install一下。
试试块maven中集成jetty把.jetty支持热部署的
‘肆’ maven使用windows xp dos命令创建java web项目出错。 已经安装apache-maven-3.0.5并配置好环境。
mvn 后面加个 -X 不行就加个 -e 就可以看到详细的报错了。
貌似你配置的插件没有能够成功下载,也无法链接到世界服务器上去。
‘伍’ 如何用maven的eclipse插件打包
现在一般都使用Maven来管理工程,包括依赖的jar包,依赖的jar包,只需要在pom.xml的元素中添加项,每一个项都是一个依赖的jar包。
当开发完之后,需要打包成可执行的jar包,那么在eclipse中如何使用Maven打包呢,并且包含POM.xml中依赖的jar包?两个步骤:
在项目的POM.xml文件中添加maven-assembly-plugin插件,如下:
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>com.hey.main.Main</mainClass>
- </manifest>
- </archive>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- </plugin>
- </plugins> </build>123456789101112131415161718
然后[右键]项目,选择[Run As]或[Debug As]的[Maven build …]后,在弹出的[Edit Configuration]的[Goals]填写assembly:assembly后,点击[Run],这样便会在项目下的[target]文件夹中生成两个jar文件,一个xxx.jar与xxx-jar-with-dependencies.jar, xxx便是你在创造Maven项目时的artifactId与version的拼接,即POM.xml文件中的与元素。这样xxx.jar便是不包含依赖的jar,xxx-jar-with-dependencies.jar便是包含依赖的jar包。
如果在终端或者shell文件中使用java命令运行,便需要运行xxx-jar-with-dependencies.jar,不然会报依赖的包不存在的错误。使用java -jar xxx-jar-with-dependencies.jar arg1, arg2 … 1>info.log 2 > err.log 运行jar包,1>info.jar表示将运行时jar包中的终端输出到info.log文件中,2>err.log表示将运行时的错误信息输出到err.log文件中。
- 一、导出到默认目录 target/dependency12
其中mainClass是你项目的入口main函数所在的类。
可以参见:http://blog.csdn.net/symgdwyh/article/details/6081532
Eclipse插件Maven打包成war
在使用Maven创建java web项目时候,需要将项目打包成war包。直接[右键]项目,选择[Run As]或[Debug As]的[Maven install],这样便会在项目下的[target]文件夹中生成xxx.war文件,xxx是项目的名称。
如果该web需要在tomcat下运行,那么直接将war包拷贝到tomcat下的webapps文件夹中,这样tomcat启动时会自动解压到webapps下面,解压的文件夹名便是war的名字(不包含后缀),如果更改war文件后,重启tomcat需要先删除解压的文件夹,然后再重启tomcat,不然会是前面的代码。
如何将依赖的jar包拷贝到项目下
从Maven项目中导出项目依赖的jar包:进入工程pom.xml 所在的目录下,执行如下命令:
1、mvn dependency:-dependencies或在eclipse中,选择项目的pom.xml文件,点击右键菜单中的Run As,见下图红框中,在弹出的Configuration窗口中,输入 dependency:-dependencies后,点击运行;
2、maven项目所依赖的jar包会导出到targed/dependency目录中。
二、导出到自定义目录中
在maven项目下创建lib文件夹,输入以下命令:
1、mvn dependency:-dependencies -DoutputDirectory=lib;
2、maven项目所依赖的jar包都会复制到项目目录下的lib目录下。
三、设置依赖级别
同时可以设置依赖级别,通常使用compile级别
mvn dependency:-dependencies -DoutputDirectory=lib -DincludeScope=compile
‘陆’ ServletContainerInitializer加载机制
最近优化项目架构针对Logback日志框架需要结合Spring的profiles配置文件达到不同运行环境自动配置参数.
最开始想法是使用ServletContextListener监听器实现在项目初始化时读取Spring的profiles配置文件手动放入当前环境变量中但在测试过程中发现问题!
环境变量还没有开始加载, Logback框架已经开始被加载!
跟踪日志信息发现一个类, Logback是通过该类进行Web环境初始化的!该类实现了javax.servlet.ServletContainerInitializer接口
第一次接触ServletContainerInitializer查找了一下相关规范, 于是自己也写了一个ServletContainerInitializer接口的类LogInitializer在但发现放置WebContent/META-INF/services/javax.servlet.ServletContainerInitializer文件无法被读取, 网上说只有放置在WebContent/WEB-INF/lib下的Jar包中才能被读取!
单独建立一个新的项目通过Maven引入到原有项目中, 忽然发现和LogInitializer存在先后加载顺序问题, 经过几次测试发现跟Jar包名称的先后顺序有关, 于是开始跟踪Tomcat运行时的Debug调试看看Tomcat是怎么加载ServletContainerInitializer实例的!
开发工具: IDEA 2017.2.6版
Servlet容器: Tomcat 8.5.20
在LogInitializer#onStartup()方法第一行打断点等待
进入断点后观察调用堆栈, 发现是org.apache.catalina.core.StandardContext#startInternal() 调用了LogInitializer#onStartup()
进入org.apache.catalina.core.StandardContext#startInternal()方法, 发现一个成员变量迭代调用
跟踪成员属性 initializers 发现是一个链表哈希映射
继续根据 initializers 被调用情况, 发现org.apache.catalina.core.StandardContext#()向成员属性中放入实例
查找org.apache.catalina.core.StandardContext#()方法被调用情况, 发现在org.apache.catalina.startup.ContextConfig#webConfig()方法中一个成员属性迭代调用
跟踪 initializerClassMap 成员属性发现类似于刚才的initializers
跟踪 initializerClassMap 被放入对象, 发现org.apache.catalina.startup.ContextConfig#()方法
进入org.apache.catalina.startup.WebappServiceLoader#load()方法观察ServletContainerInitializer实例被加载情况
观察了一下 javax.servlet.ServletContext.ORDERED_LIBS 静态常量发现与web.xml中的<absolute-ordering>元素相关, 因为没有配置所以先不考虑, 这样判断 orderedLibs!=null 不会被执行类加载器loader的引用就是当前项目Servlet容器的类加载器去加载资源
查找到五个资源, Tomcat容器自身2个, LogInitializer, , (Spring的) 这个时候已经明了ServletContainerInitializer一般情况下是基于类加载器加载资源的枚举迭代顺序, 因为委托加载机制会从最顶级类加载向下一直加载到当前项目Web容器类加载器下的资源, 到此基本已经弄清楚ServletContainerInitializer加载流程!
因为看见了 ORDERED_LIBS 常量所以想继续研究一下<absolute-ordering>元素, 大概看了一下相关规范文档定义, 该元素是Servlet3.0出现的为第三方插件Jar包自动配置一些Servlet, Filter时所有第三方插件按照指定绝对顺序加载Jar包资源
1. 在Jar包的META-INF目录下建立web-fragment.xml
2, 配置web.xml
在org.apache.catalina.startup.WebappServiceLoader#load()方法打断点, 发现找到一个jar包
将当前项目 Web容器类加载器 替换为 Tomca容器类加载器 是因为委托加载机制从最顶级类加载器加载到当前类加载器所以会导致当前项目Web容器不会被加载到, 就会跳过当前项目下WEB-INF/lib中的Jar包查找所以只有log-initializer-0.1.jar中的ServletContainerInitializer被加载, , 的配置丢失
不符合预期结果, 继续看<absolute-ordering>文档, 发现一个<others />元素, 在<absolute-ordering>中追加
重新开始在org.apache.catalina.startup.WebappServiceLoader#load()方法断点Debug
这次就有所有Jar包了, 但很好奇去项目部署的Tomcat下WEB-INF/lib 看了一下Jar包数量 44个 而非37个逐一匹配发现缺少这些Jar包
非常好奇为什么会丢失Jar包, 如果只是丢失apache相关的也可能Tomcat排除自身lib下相关的, 但是slf4j, aspecj居然也没有, 本着求知的好奇心继续调试, 找出缺少Jar包的原因
因为是通过 ORDERED_LIBS 常量获取的容器属性值, 所以直接在org.apache.catalina.core.ApplicationContext#setAttribute()使用条件断点
查看堆栈信息, 发现被org.apache.tomcat.util.descriptor.web.WebXml#orderWebFragments()方法调用
跟踪局部变量 orderedFragments
发现是局部变量 fragments 映射迭代放入, 而 fragments 是由外部传递
继续向上跟踪堆栈
找到org.apache.tomcat. JarScanner 实现类org.apache.tomcat.util.scan. StandardJarScanner #scan()方法
找到org.apache.tomcat. JarScanFilter 实现类org.apache.tomcat.util.scan. StandardJarScanFilter #check()方法
至此基本上算是了解ServletContainerInitializer加载机制!
‘柒’ 怎么编译运行从github上下载下来的maven项目源码(我想调试运行的是sonarqube的源码)跪求大神orz
maven 编译命令
从别处拷贝来的maven项目,有时候会出现不能debug关联的情况,即按住ctrl键后不能跳转到相应的类或方法,这是需要eclipse环境生成 Eclipse 项目文件的。
可以在cmd命令窗口下进入到maven项目包含pom.xml文件的目录下,运行 mvn eclipse:eclipse 命令即可,如果还不行,可使用mvn compile :编译源代码 命令。
--------------------------------------------------------------------------------------------------------------------------------
下面是一些maven的常用命令:
Maven2 的运行命令为 : mvn ,
常用命令为 :
mvn archetype:create :创建 Maven 项目
mvn compile :编译源代码
mvn test-compile :编译测试代码
mvn test : 运行应用程序中的单元测试
mvn site : 生成项目相关信息的网站
mvn clean :清除目标目录中的生成结果
mvn package : 依据项目生成 jar 文件
mvn install :在本地 Repository 中安装 jar
mvn eclipse:eclipse :生成 Eclipse 项目文件
生成项目
建一个 JAVA 项目 : mvn archetype:create -DgroupId=com.demo -DartifactId=App
建一个 web 项目 : mvn archetype:create -DgroupId=com.demo -DartifactId=web-app -DarchetypeArtifactId=maven-archetype-webapp
生成 Eclipse 项目
普通 Eclipse 项目执行 : mvn eclipse:eclipse
Eclipse WTP 项目执行 : mvn eclipse:eclipse –Dwtpversion=1.0
更详细的,自己网络搜索 maven 编译,学习一下吧
‘捌’ 我用idea断点调试javaweb项目,断点是一次性的吗idea版本是2016的
我也遇到了这样的问题只能重新设置断点啦
‘玖’ liberoffce生成excel
Windows下使用LibreOffice将office文档(word,ppt,excel)转成PDF(Java)
第一步:安装Windows版LibreOffice
1.1去官网LibreOffice
https://zh-cn.libreoffice.org/
1.2下载
1.2.1第一步:进入官网点击下载
第二步:使用java代码操作LibreOffice进行(doc,ppt,xls)文件转换成PDF文件
1.1:打开IDEA或者eclipse创建一个SpringBoot项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>filepreview</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<!--Springmvc启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.liumapp.workable.converter</groupId>
<artifactId>workable-converter</artifactId>
<version>v1.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>0
1.2创建一个类ConvertOfficePDFUtils.java这个文件名称自己取
/**
* @description libreoffice工具类
* @author holle
* @create 2020/12/24
* @since 1.0.0
*/package com.file.preview.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;import java.io.IOException;import java.io.InputStream;@Servicepublic class ConvertOfficePDFUtils {
private static Logger logger=LoggerFactory.getLogger(ConvertOfficePDFUtils.class);
/**
* 利用libreOffice将office文档转换成pdf
* @param inputFile 目标文件地址
* @param pdfFile 输出文件夹
* @return
*/
public static boolean convertOfficePDF(String inputFile,String pdfFile){
long start=System.currentTimeMillis();
String command;
boolean flag;
String osName=System.getProperty("os.name");
if (osName.contains("Windows")){
command="cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
}else {
command="libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
}
flag =executeLibreOfficeCommand(command);
long end =System.currentTimeMillis();
logger.debug("用时:{} ms", end - start);
return flag;
}
/**
* 执行command指令
* @param command
* @return
*/
public static boolean executeLibreOfficeCommand(String command) {
logger.info("开始进行转化.......");
Process process;// Process可以控制该子进程的执行或获取该子进程的信息
try {
logger.debug("convertOffice2PDF cmd : {}", command);
process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。
// 下面两个可以获取输入输出流
InputStream errorStream = process.getErrorStream();
InputStream inputStream = process.getInputStream();
} catch (IOException e) {
logger.error(" convertOffice2PDF {} error", command, e);
return false;
}
int exitStatus = 0;
try {
exitStatus = process.waitFor();// 等待子进程完成再往下执行,返回值是子线程执行完毕的返回值,返回0表示正常结束
// 第二种接受返回值的方法
int i = process.exitValue(); // 接收执行完毕的返回值
logger.debug("i----" + i);
} catch (InterruptedException e) {
logger.error("InterruptedException convertOffice2PDF {}", command, e);
return false;
}
if (exitStatus != 0) {
logger.error("convertOffice2PDF cmd exitStatus {}", exitStatus);
} else {
logger.debug("convertOffice2PDF cmd exitStatus {}", exitStatus);
}
process.destroy(); // 销毁子进程
logger.info("转化结束.......");
return true;
}}576777879
1.3创建一个测试类
/**
* @description 测试类
* @author holle
* @create 2020/12/24
* @since 1.0.0
*/package com.test;import com.file.preview.FilePreviewApplication;import com.file.preview.utils.ConvertOfficePDFUtils;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;import java.io.IOException;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = FilePreviewApplication.class)public class TestOffice {
@Resource
private ConvertOfficePDFUtils convertOfficePDFUtils;
@Test
public void JunintTestPDF(){
long start =System.currentTimeMillis();
String srcPath ="C:/Users/holler/Desktop/*/*.docx",desPath="C:/Users/holler/Desktop/*";
String command="";
String osName=System.getProperty("os.name");
if (osName.contains("Windows")){
//输入cmd命令;添加:srcPath表示文件所在路径;desPath:文件输出的路径
command="soffice --headless --convert-to pdf " + srcPath + " --outdir " + desPath;
convertOfficePDFUtils.executeLibreOfficeCommand(command);
}
long end=System.currentTimeMillis();
System.out.println("用时:{} ms"+(end-start));
}
}
Path + " --outdir " + desPath;
convertOfficePDFUtils.executeLibreOfficeCommand(command);
}
long end=System.currentTimeMillis();
System.out.println(“用时:{} ms”+(end-start));
}
}
‘拾’ Maven多Web模块怎么调试
tomcat好像只有部署的形式,不能以容器直接启动?反正搞了半天 断点就是打不上
给你jetty的配置
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<!-- 热部署 -->
<scanIntervalSeconds>0</scanIntervalSeconds>
<httpConnector>
<port>9090</port>
</httpConnector>
<!-- <webAppConfig> <contextPath>/${project.artifactId}</contextPath>
</webAppConfig> -->
</configuration>
</plugin>
关于版本 ${jetty.version}
你可以去:http://search.maven.org/#search%7Cga%7C1%7Corg.eclipse.jetty
找一个 我用的是最新的 9.2.0.M0
关于jetty配置可以看:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
然后build里写 jetty:run 就行了
http://www.oschina.net/question/106591_151036?sort=time