Idea Java Gradle创建项目,添加依赖,引用本地jar,打包jar
2020年最后一天,没什么重要的事,就简单研究研究Idea的Gradle
idea版本:2020.2
首先创建项目,选Gradle->java
填好项目名和包名
然后Finish
创建好后发现没有包和Main.java类,只好自己创建
右上角没法run
在Main.java类内右键->Run 'Main.main()'
看到输出test
项目运行成功,试着在build.gradle添加okhttp
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("com.squareup.okhttp3:okhttp:4.9.0")
}
按ctrl+shift+o,刷新一下或者按下下图按钮
写上okhttp代码测试一下
package com.hyq.hm.test.gradle;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Objects;
public class Main {
public static void main(String[] args){
System.out.println("test");
String url = "http://wwww.baidu.com";
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
System.out.println("onFailure");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
System.out.println("onResponse: " + Objects.requireNonNull(response.body()).string());
}
});
}
}
run一下
成功
接下来添加本地jar包了,就拿opencv 4.4.0来举例
首先在项目目录内创建一个lib文件夹
将opencv的jar包复制到lib文件夹内
点击idea的右上角的Edit Configurations...
在VM options:内填上opencv_java440.dll的文件夹路径
在build.gradle的dependencies内添加代码
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile("com.squareup.okhttp3:okhttp:4.9.0")
compile fileTree(dir:'lib',includes:['*jar'])
}
按ctrl+shift+o刷新一下
创建ImageGUI.java来显示,也可以跳过这个,我只是显示看看
package com.hyq.hm.test.gradle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
public class ImageGUI extends JComponent{
private BufferedImage image;
@Override
protected void paintComponent(Graphics g) {
int width = g.getClipBounds().width;
int height = g.getClipBounds().height;
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
if(image != null) {
int[] rect = viewportSize(width, height,image.getWidth(),image.getHeight());
g2d.drawImage(image, rect[0], rect[1], rect[2], rect[3], null);
}
}
private JFrame ui;
public void createWin(String title) {
ui = new JFrame();
ui.setLayout(null);
ui.setTitle(title);
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int viewWidth = 800;
int viewHeight = (int) (9.0f/16.0f*viewWidth);
setBounds(0,0,viewWidth,viewHeight);
ui.add(this);
ui.setSize(viewWidth,viewHeight);
ui.setVisible(true);
ui.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int width = ui.getSize().width;
int height = ui.getSize().height;
setSize(width, height);
}
});
}
private int[] viewportSize(int screenWidth, int screenHeight, int width, int height) {
int left, top, viewWidth, viewHeight;
float sh = screenWidth * 1.0f / screenHeight;
float vh = width * 1.0f / height;
if (sh < vh) {
left = 0;
viewWidth = screenWidth;
viewHeight = (int) (height * 1.0f / width * viewWidth);
top = (screenHeight - viewHeight) / 2;
} else {
top = 0;
viewHeight = screenHeight;
viewWidth = (int) (width * 1.0f / height * viewHeight);
left = (screenWidth - viewWidth) / 2;
}
return new int[]{left, top, viewWidth, viewHeight};
}
public void imshow(BufferedImage image) {
this.image = image;
this.repaint();
}
}
在Main.java内添加代码
package com.hyq.hm.test.gradle;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
public class Main {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args){
System.out.println("test");
Mat mat = new Mat(100,100, CvType.CV_8UC3,new Scalar(255,0,0));
ImageGUI gui = new ImageGUI();
gui.createWin("test");
gui.imshow(toImage(mat));
}
public static BufferedImage toImage(Mat matrix) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (matrix.channels() == 3) {
type = BufferedImage.TYPE_3BYTE_BGR;
}else if(matrix.channels() == 4){
type = BufferedImage.TYPE_4BYTE_ABGR;
}
int bufferSize = matrix.channels() * matrix.cols() * matrix.rows();
byte[] buffer = new byte[bufferSize];
matrix.get(0, 0, buffer);
BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
return image;
}
}
run一下
好了,引用本地jar也结束了,该打包了
对build.gradle内进行修改和添加代码
apply plugin: 'java'
apply plugin: 'idea'
group 'com.hyq.hm.test.gradle'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("com.squareup.okhttp3:okhttp:4.9.0")
compile fileTree(dir:'lib',includes:['*jar'])
}
jar {
String someString = ''
configurations.runtime.each { someString = someString + " lib\\" + it.name }
manifest {
attributes 'Main-Class': 'com.hyq.hm.test.gradle.Main'
attributes 'Class-Path': someString
}
}
task clearPj(type:Delete){
delete 'build','target'
}
task copyJar(type:Copy){
from configurations.runtime
into ('build/libs/lib')
}
task release(type: Copy,dependsOn: [build,copyJar]){
}
按ctrl+shift+o刷新一下
然后View->Tool Windows->Gradle
双击release
等编译结束后在项目的build文件夹内多出了一个libs文件夹,点开
将opencv_java440.dll也拷贝进来
打开控制台运行一下
打包部分参考文章:最全面的使用idea和gradle把java项目打包成jar的方法
本文地址:https://blog.csdn.net/u010302327/article/details/112005370