如何用MSBuild打包IronRuby项目生成可执行文件?
IronRuby项目打包为.exe文件的解决方案
问题背景
我有一个用IronRuby在SharpDevelop中创建的MyProject.sln解决方案,需要打包成.exe文件。按网上教程下载了MSBuild 14.0后,在管理员命令行执行了以下命令:
C:\windows\system32>cd C:\Users\UserName\myproject Users\UserName\myproject>"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" MyProject.sln /p:Configuration=Release
执行后输出显示构建成功:
Build projects in this solution one at a time. To enable parallel building, add the "/m" parameter. Project "C:\Users\UserName\myproject\MyProject.sln" in node 1 (target objects by default) ValidateSolutionConfiguration: Creating a "Release |Any CPU" solution configuration The project "C:\Users\UserName\myproject\MyProject.sln" built (target objects by default) The build has completed successfully Warnings: 0 Errors: 0
但始终没有生成.exe文件,附上我的.rbproj文件内容:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <ProjectGuid>{A0054952-C176-4FBC-A53F-0DD691E6F33E}</ProjectGuid> <Configuration Condition=" '$(Configuration)' == 'Release' ">Release</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <OutputPath>bin\Release\</OutputPath> <OutputType>WinExe</OutputType> <RootNamespace>my_project</RootNamespace> <AssemblyName>my-project</AssemblyName> <MainFile>Program.rb</MainFile> </PropertyGroup> <PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' "> <PlatformTarget>x86</PlatformTarget> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <OutputPath>bin\Debug\</OutputPath> <DebugSymbols>True</DebugSymbols> <DebugType>Full</DebugType> <Optimize>False</Optimize> <DebugInfo>True</DebugInfo> <StartArguments>--disable-gems -D Program.rb</StartArguments> <StartWorkingDirectory>.</StartWorkingDirectory> <StartAction>Program</StartAction> <StartProgram>C:\Program Files (x86)\SharpDevelop\4.4\bin\..\AddIns\BackendBindings\RubyBinding\ir.exe</StartProgram> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <OutputPath>bin\Release\</OutputPath> <DebugSymbols>False</DebugSymbols> <DebugType>None</DebugType> <Optimize>True</Optimize> <StartArguments>--disable-gems Program.rb</StartArguments> <StartWorkingDirectory>.</StartWorkingDirectory> <StartAction>Program</StartAction> <StartProgram>C:\Program Files (x86)\SharpDevelop\4.4\bin\..\AddIns\BackendBindings\RubyBinding\ir.exe</StartProgram> </PropertyGroup> <ItemGroup> <Reference Include="IronRuby"> <HintPath>C:\Program Files %28x86%29\SharpDevelop\4.4\bin\..\AddIns\BackendBindings\RubyBinding\IronRuby.dll</HintPath> </Reference> <Reference Include="mscorlib" /> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Drawing" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Form1.rb" /> <Compile Include="Form2.rb" /> <Compile Include="Form3.rb" /> <Compile Include="MainForm.rb" /> <Compile Include="Program.rb" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="Form1.resx"> <DependentUpon>Form1.rb</DependentUpon> </EmbeddedResource> <EmbeddedResource Include="Form2.resx"> <DependentUpon>Form2.rb</DependentUpon> </EmbeddedResource> <EmbeddedResource Include="MainForm.resx"> <DependentUpon>MainForm.rb</DependentUpon> </EmbeddedResource> </ItemGroup> <Target Name="Build" /> </Project>
解决步骤
1. 修复.rbproj中的空Build目标
你的.rbproj里的<Target Name="Build" />是空节点,MSBuild执行时根本没做实际的编译工作。替换这个节点为以下内容:
<Target Name="Build"> <!-- 配置IronRuby工具路径,根据你的SharpDevelop安装路径调整 --> <PropertyGroup> <IronRubyDir>C:\Program Files (x86)\SharpDevelop\4.4\AddIns\BackendBindings\RubyBinding</IronRubyDir> <IrcExePath>$(IronRubyDir)\irc.exe</IrcExePath> <OutputAssembly>$(OutputPath)$(AssemblyName).exe</OutputAssembly> </PropertyGroup> <!-- 调用IronRuby编译器irc.exe生成可执行文件 --> <Exec Command=""$(IrcExePath)" --compile --output="$(OutputAssembly)" --target=$(OutputType) $(MainFile) @(Compile->'"%(FullPath)"')" /> <!-- 复制依赖的IronRuby核心库到输出目录 --> <Copy SourceFiles="$(IronRubyDir)\IronRuby.dll" DestinationFolder="$(OutputPath)" /> <Copy SourceFiles="$(IronRubyDir)\IronRuby.Libraries.dll" DestinationFolder="$(OutputPath)" /> <Copy SourceFiles="$(IronRubyDir)\Microsoft.Scripting.dll" DestinationFolder="$(OutputPath)" /> <Copy SourceFiles="$(IronRubyDir)\Microsoft.Scripting.Core.dll" DestinationFolder="$(OutputPath)" /> </Target>
注意:如果你的SharpDevelop安装路径不同,要修改IronRubyDir的值,确保指向正确的IronRuby插件目录。
2. 重新运行MSBuild命令
再次执行之前的构建命令:
cd C:\Users\UserName\myproject "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" MyProject.sln /p:Configuration=Release
完成后,你会在C:\Users\UserName\myproject\bin\Release目录下找到my-project.exe,以及运行所需的依赖DLL。
3. 可选:打包成单文件或MSI安装包
- 单文件打包:用ILMerge工具把所有依赖合并成一个独立.exe,命令示例:
ILMerge.exe /out:MyProjectSingle.exe /target:winexe my-project.exe IronRuby.dll IronRuby.Libraries.dll Microsoft.Scripting.dll Microsoft.Scripting.Core.dll - MSI安装包:可以用Visual Studio的安装项目模板,或者WiX Toolset,把.exe和依赖文件打包成MSI,方便分发。
内容的提问来源于stack exchange,提问作者Mila1803
相关产品推荐
相关产品推荐

