运行JavaApplication

Studio利用hbm配置文件生成SQL文件时,需要访问ST项目中生成的POJO类(多对多关联时,要访问中间表的PO类),所以要把代码生成器org.st.studio.generator.SqlGenerator(包含public static void main(String[] args)可以作为Java Application运行)放在ST项目中进行加载(关于如何在项目中加载插件提供的jar包,请参考《为Java项目加载类路径》),再模拟创建一个代码生成器的Java Application,然后把相关参数传给Application,最后通过运行这个Application来生成SQL文件。

public void runJavaApplication() throws CodeGenerationException { try { if (getStProject() == null) return; IJavaProject jp = JavaCore.create(getStProject().getProject()); IType type = jp.findType(SQL_GENERATOR_CLASS); String prgArgs = ""; //设置运行Java Application需要的参数,可以有多个参数,参数之间用” ”空格分开 。。。。。。 ILaunchConfiguration lc = createLaunchConfiguration(type, prgArgs); ILaunch launch = lc.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor()); IProcess process = launch.getProcesses()[0]; //任何时候只有一个过程 //等待直到LaunchConfiguration运行完成 while(!process.isTerminated()){ ; } } catch (Exception e) { Logger.log(e); } } private ILaunchConfiguration createLaunchConfiguration(IType type, String prgArgs) throws CoreException { ILaunchConfigurationType launchType = DebugPlugin.getDefault() .getLaunchManager().getLaunchConfigurationType( "org.eclipse.jdt.launching.localJavaApplication"); ILaunchConfigurationWorkingCopy config = launchType.newInstance(null, "A SqlGenerator"); // 设置此launch配置是私有的,也就是用户不可见 config.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true); config.setAttribute( IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type .getFullyQualifiedName()); config.setAttribute( IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, true); config.setAttribute( IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, getSotowerProject().getProject().getName()); config.setAttribute( IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, prgArgs); return config; }

 

 

 

 

代码的1319行的运行ILaunchConfiguration并获得第一个IProcess(任何时候只有一个SqlGeneratorJavaApplication在运行),然后一直等待直到IProcess运行结束。还可以通过另一种方式来运行JavaApplication

           LaunchUtil.launchInBackground(lc, ILaunchManager.RUN_MODE);

这种方式将在后台令起一个线程来运行SqlGeneratorJavaApplication

但是场景的代码生成应该是个整体,各种文件依次生成,然后场景的代码生成结束,最后对ST项目进行刷新。如果采用第二种方式,SQL文件的生成就会独立于整个场景的代码生成,其他文件都生成完成,刷新ST项目的时候,SQL文件可能还没有生成,就会导致SQL文件不会显示在ST项目中,需要用户自己再次手动刷新才能看到。所以在程序中采用的是第一种方式。LaunchUtil类的源代码请参见附件。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值