[英]Compiling library with ant script
我创建了Point
包(项目),其中包含Square
, Rectangle
, Point
, Circle
和Line
。 它们是简单的类,带有用于创建所述对象的构造函数。 在Point in main中,您可以这样称呼他们:
Point p1 = new Point(0,3);
我应该编写一个程序,要求用户选择要创建的对象并设置其几何形状,并且我只能将Point包用作库。
1)创建simple-graphics.jar
库。 我从Point
包中删除了main方法,并设法从Point
包中生成了simple-graphics.jar
可执行文件.jar文件。
2)我被要求创建一些蚂蚁脚本,从其源文件编译该库并生成.jar文件,但是,我不知道该怎么做,如果我在1)中还没有做过,关于蚂蚁脚本的教程是我不太清楚。 我猜我应该通过在NetBeans中选择Produce .jar选项并在某处放置此ant脚本来实现这两种方式。
3)我应该能够通过使用java -jar simple-graphics.jar
运行2)生成的.jar文件,如何在NetBeans中做到这一点,还是应该使用cmd? 我在W7。
编辑:谢谢您的脚本,作为一种看,我肯定无法编写所有脚本。
如何在程序中使用此库? 解决-像这样:
package simpleapp;
import point;
public class SimpleApp{
public static void main(String [] args){
//Please press 1 to create Point
//Please specify x and y axis:
//i will select the type of object and create it
//Object o = new whichObject(1)(x,y);
}
}
包Point中的Point类,其他类非常相似:
package point;
public class Point{
double x;
double y;
public Point(double a, double b){
x = a;
y = b;
}
public Point(){
x = 0;
y = 0;
}
public double distance(Point p){
return Math.sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y-y));
}
}
这是一个非常基本的Ant脚本( build.xml
),用于将Java文件编译为类文件,并将它们打包到JAR文件中,前提是您的Java源文件位于子目录src
。 请注意,这只是一个起点。
<project name="Point-Library" default="build">
<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="jar.name" value="simple-graphics.jar" />
<target name="build" depends="prepare, compile, jar" />
<target name="prepare" description="Creates the build folder">
<mkdir dir="${build.dir}" />
</target>
<target name="compile" description="Compiles the Java source files">
<javac srcdir="${src.dir}" destdir="${build.dir}" />
</target>
<target name="jar" description="Packs the compiled Java classes into a JAR file">
<jar basedir="${build.dir}" destfile="${jar.name}" />
</target>
</project>
但是,您首先应该阅读Ant官方手册以了解这些概念。
要使用生成的JAR文件,您必须将其添加到您的类路径中(您已经这样做了)。 然后,您可以导入打包point
。
package simpleapp;
import point; // <---
public class SimpleApp{
public static void main(String [] args){
//Please press 1 to create Point
//Please specify x and y axis:
//i will select the type of object and create it
//Object o = new whichObject(1)(x,y);
}
}
对于使用point
类的客户端代码,无论其源代码是项目的一部分,还是仅导入包含point
类文件的JAR文件,这都没有区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.